[FL-2605] NFC new design (#1364)
* nfc: add new read scene * lib: refactore nfc library * mifare desfire: add read card fuction * lib nfc: add auto read worker * nfc: add supported cards * nfc: add mifare classic read success scene * nfc: add troyka support * submodule: update protobuf * nfc: mifare classic keys cache * nfc: rework mifare classic key cache * Correct spelling * nfc: add user dictionary * nfc: introduce block read map in fff * nfc: rework dict attack * nfc: improve dict attack * nfc: rework mifare classic format * nfc: rework MFC read with Reader * nfc: add gui for MFC read success scene * nfc: fix dict attack view gui * nfc: add retry and exit confirm scenes * nfc: add retry and exit scenes navigation * nfc: check user dictionary * nfc: remove unused scenes * nfc: rename functions in nfc worker * nfc: rename mf_classic_dict_attack -> dict_attack * nfc: change scenes names * nfc: remove scene tick events * nfc: rework dict calls with buffer streams * nfc: fix notifications * nfc: fix mf desfire navigation * nfc: remove notification from mf classic read success * nfc: fix read sectors calculation * nfc: add fallback for unknown card * nfc: show file name while emulating * nfc: fix build * nfc: fix memory leak * nfc: fix desfire read * nfc: add no dict found navigation * nfc: add read views * nfc: update card fix * nfc: fix access bytes save * nfc: add exit and retry confirm to mf ultralight read success * nfc: introduce detect reader * nfc: change record open arg to macros * nfc: fix start from archive Co-authored-by: Astra <astra@astrra.space> Co-authored-by: あく <alleteam@gmail.com>
This commit is contained in:
16
lib/nfc/SConscript
Normal file
16
lib/nfc/SConscript
Normal file
@@ -0,0 +1,16 @@
|
||||
Import("env")
|
||||
|
||||
env.Append(
|
||||
CPPPATH=[
|
||||
"#/lib/nfc",
|
||||
],
|
||||
)
|
||||
|
||||
libenv = env.Clone(FW_LIB_NAME="nfc")
|
||||
libenv.ApplyLibFlags()
|
||||
|
||||
sources = libenv.GlobRecursive("*.c*")
|
||||
|
||||
lib = libenv.StaticLibrary("${FW_LIB_NAME}", sources)
|
||||
libenv.Install("${LIB_DIST_DIR}", lib)
|
||||
Return("lib")
|
||||
148
lib/nfc/helpers/mf_classic_dict.c
Normal file
148
lib/nfc/helpers/mf_classic_dict.c
Normal file
@@ -0,0 +1,148 @@
|
||||
#include "mf_classic_dict.h"
|
||||
|
||||
#include <lib/toolbox/args.h>
|
||||
#include <lib/flipper_format/flipper_format.h>
|
||||
|
||||
#define MF_CLASSIC_DICT_FLIPPER_PATH EXT_PATH("nfc/assets/mf_classic_dict.nfc")
|
||||
#define MF_CLASSIC_DICT_USER_PATH EXT_PATH("nfc/assets/mf_classic_dict_user.nfc")
|
||||
|
||||
#define TAG "MfClassicDict"
|
||||
|
||||
#define NFC_MF_CLASSIC_KEY_LEN (13)
|
||||
|
||||
struct MfClassicDict {
|
||||
Stream* stream;
|
||||
uint32_t total_keys;
|
||||
};
|
||||
|
||||
bool mf_classic_dict_check_presence(MfClassicDictType dict_type) {
|
||||
Storage* storage = furi_record_open(RECORD_STORAGE);
|
||||
|
||||
bool dict_present = false;
|
||||
if(dict_type == MfClassicDictTypeFlipper) {
|
||||
dict_present = storage_common_stat(storage, MF_CLASSIC_DICT_FLIPPER_PATH, NULL) == FSE_OK;
|
||||
} else if(dict_type == MfClassicDictTypeUser) {
|
||||
dict_present = storage_common_stat(storage, MF_CLASSIC_DICT_USER_PATH, NULL) == FSE_OK;
|
||||
}
|
||||
|
||||
furi_record_close(RECORD_STORAGE);
|
||||
|
||||
return dict_present;
|
||||
}
|
||||
|
||||
MfClassicDict* mf_classic_dict_alloc(MfClassicDictType dict_type) {
|
||||
MfClassicDict* dict = malloc(sizeof(MfClassicDict));
|
||||
Storage* storage = furi_record_open(RECORD_STORAGE);
|
||||
dict->stream = buffered_file_stream_alloc(storage);
|
||||
furi_record_close(RECORD_STORAGE);
|
||||
|
||||
bool dict_loaded = false;
|
||||
do {
|
||||
if(dict_type == MfClassicDictTypeFlipper) {
|
||||
if(!buffered_file_stream_open(
|
||||
dict->stream, MF_CLASSIC_DICT_FLIPPER_PATH, FSAM_READ, FSOM_OPEN_EXISTING)) {
|
||||
buffered_file_stream_close(dict->stream);
|
||||
break;
|
||||
}
|
||||
} else if(dict_type == MfClassicDictTypeUser) {
|
||||
if(!buffered_file_stream_open(
|
||||
dict->stream, MF_CLASSIC_DICT_USER_PATH, FSAM_READ_WRITE, FSOM_OPEN_ALWAYS)) {
|
||||
buffered_file_stream_close(dict->stream);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// Read total amount of keys
|
||||
string_t next_line;
|
||||
string_init(next_line);
|
||||
while(true) {
|
||||
if(!stream_read_line(dict->stream, next_line)) break;
|
||||
if(string_get_char(next_line, 0) == '#') continue;
|
||||
if(string_size(next_line) != NFC_MF_CLASSIC_KEY_LEN) continue;
|
||||
dict->total_keys++;
|
||||
}
|
||||
string_clear(next_line);
|
||||
stream_rewind(dict->stream);
|
||||
|
||||
dict_loaded = true;
|
||||
FURI_LOG_I(TAG, "Loaded dictionary with %d keys", dict->total_keys);
|
||||
} while(false);
|
||||
|
||||
if(!dict_loaded) {
|
||||
buffered_file_stream_close(dict->stream);
|
||||
free(dict);
|
||||
dict = NULL;
|
||||
}
|
||||
|
||||
return dict;
|
||||
}
|
||||
|
||||
void mf_classic_dict_free(MfClassicDict* dict) {
|
||||
furi_assert(dict);
|
||||
furi_assert(dict->stream);
|
||||
|
||||
buffered_file_stream_close(dict->stream);
|
||||
stream_free(dict->stream);
|
||||
free(dict);
|
||||
}
|
||||
|
||||
uint32_t mf_classic_dict_get_total_keys(MfClassicDict* dict) {
|
||||
furi_assert(dict);
|
||||
|
||||
return dict->total_keys;
|
||||
}
|
||||
|
||||
bool mf_classic_dict_get_next_key(MfClassicDict* dict, uint64_t* key) {
|
||||
furi_assert(dict);
|
||||
furi_assert(dict->stream);
|
||||
|
||||
uint8_t key_byte_tmp = 0;
|
||||
string_t next_line;
|
||||
string_init(next_line);
|
||||
|
||||
bool key_read = false;
|
||||
*key = 0ULL;
|
||||
while(!key_read) {
|
||||
if(!stream_read_line(dict->stream, next_line)) break;
|
||||
if(string_get_char(next_line, 0) == '#') continue;
|
||||
if(string_size(next_line) != NFC_MF_CLASSIC_KEY_LEN) continue;
|
||||
for(uint8_t i = 0; i < 12; i += 2) {
|
||||
args_char_to_hex(
|
||||
string_get_char(next_line, i), string_get_char(next_line, i + 1), &key_byte_tmp);
|
||||
*key |= (uint64_t)key_byte_tmp << 8 * (5 - i / 2);
|
||||
}
|
||||
key_read = true;
|
||||
}
|
||||
|
||||
string_clear(next_line);
|
||||
return key_read;
|
||||
}
|
||||
|
||||
bool mf_classic_dict_rewind(MfClassicDict* dict) {
|
||||
furi_assert(dict);
|
||||
furi_assert(dict->stream);
|
||||
|
||||
return stream_rewind(dict->stream);
|
||||
}
|
||||
|
||||
bool mf_classic_dict_add_key(MfClassicDict* dict, uint8_t* key) {
|
||||
furi_assert(dict);
|
||||
furi_assert(dict->stream);
|
||||
|
||||
string_t key_str;
|
||||
string_init(key_str);
|
||||
for(size_t i = 0; i < 6; i++) {
|
||||
string_cat_printf(key_str, "%02X", key[i]);
|
||||
}
|
||||
string_cat_printf(key_str, "\n");
|
||||
|
||||
bool key_added = false;
|
||||
do {
|
||||
if(!stream_seek(dict->stream, 0, StreamOffsetFromEnd)) break;
|
||||
if(!stream_insert_string(dict->stream, key_str)) break;
|
||||
key_added = true;
|
||||
} while(false);
|
||||
|
||||
string_clear(key_str);
|
||||
return key_added;
|
||||
}
|
||||
28
lib/nfc/helpers/mf_classic_dict.h
Normal file
28
lib/nfc/helpers/mf_classic_dict.h
Normal file
@@ -0,0 +1,28 @@
|
||||
#pragma once
|
||||
|
||||
#include <stdbool.h>
|
||||
#include <storage/storage.h>
|
||||
#include <lib/flipper_format/flipper_format.h>
|
||||
#include <lib/toolbox/stream/file_stream.h>
|
||||
#include <lib/toolbox/stream/buffered_file_stream.h>
|
||||
|
||||
typedef enum {
|
||||
MfClassicDictTypeUser,
|
||||
MfClassicDictTypeFlipper,
|
||||
} MfClassicDictType;
|
||||
|
||||
typedef struct MfClassicDict MfClassicDict;
|
||||
|
||||
bool mf_classic_dict_check_presence(MfClassicDictType dict_type);
|
||||
|
||||
MfClassicDict* mf_classic_dict_alloc(MfClassicDictType dict_type);
|
||||
|
||||
void mf_classic_dict_free(MfClassicDict* dict);
|
||||
|
||||
uint32_t mf_classic_dict_get_total_keys(MfClassicDict* dict);
|
||||
|
||||
bool mf_classic_dict_get_next_key(MfClassicDict* dict, uint64_t* key);
|
||||
|
||||
bool mf_classic_dict_rewind(MfClassicDict* dict);
|
||||
|
||||
bool mf_classic_dict_add_key(MfClassicDict* dict, uint8_t* key);
|
||||
166
lib/nfc/helpers/nfc_debug_pcap.c
Normal file
166
lib/nfc/helpers/nfc_debug_pcap.c
Normal file
@@ -0,0 +1,166 @@
|
||||
#include "nfc_debug_pcap.h"
|
||||
|
||||
#include <furi_hal_rtc.h>
|
||||
#include <stream_buffer.h>
|
||||
|
||||
#define TAG "NfcDebugPcap"
|
||||
|
||||
#define PCAP_MAGIC 0xa1b2c3d4
|
||||
#define PCAP_MAJOR 2
|
||||
#define PCAP_MINOR 4
|
||||
#define DLT_ISO_14443 264
|
||||
|
||||
#define DATA_PICC_TO_PCD 0xFF
|
||||
#define DATA_PCD_TO_PICC 0xFE
|
||||
#define DATA_PICC_TO_PCD_CRC_DROPPED 0xFB
|
||||
#define DATA_PCD_TO_PICC_CRC_DROPPED 0xFA
|
||||
|
||||
#define NFC_DEBUG_PCAP_FILENAME EXT_PATH("nfc/debug.pcap")
|
||||
#define NFC_DEBUG_PCAP_BUFFER_SIZE 64
|
||||
|
||||
struct NfcDebugPcapWorker {
|
||||
bool alive;
|
||||
Storage* storage;
|
||||
File* file;
|
||||
StreamBufferHandle_t stream;
|
||||
FuriThread* thread;
|
||||
};
|
||||
|
||||
static File* nfc_debug_pcap_open(Storage* storage) {
|
||||
File* file = storage_file_alloc(storage);
|
||||
if(!storage_file_open(file, NFC_DEBUG_PCAP_FILENAME, FSAM_WRITE, FSOM_OPEN_APPEND)) {
|
||||
storage_file_free(file);
|
||||
return NULL;
|
||||
}
|
||||
if(!storage_file_tell(file)) {
|
||||
struct {
|
||||
uint32_t magic;
|
||||
uint16_t major, minor;
|
||||
uint32_t reserved[2];
|
||||
uint32_t snaplen;
|
||||
uint32_t link_type;
|
||||
} __attribute__((__packed__)) pcap_hdr = {
|
||||
.magic = PCAP_MAGIC,
|
||||
.major = PCAP_MAJOR,
|
||||
.minor = PCAP_MINOR,
|
||||
.snaplen = FURI_HAL_NFC_DATA_BUFF_SIZE,
|
||||
.link_type = DLT_ISO_14443,
|
||||
};
|
||||
if(storage_file_write(file, &pcap_hdr, sizeof(pcap_hdr)) != sizeof(pcap_hdr)) {
|
||||
FURI_LOG_E(TAG, "Failed to write pcap header");
|
||||
}
|
||||
}
|
||||
return file;
|
||||
}
|
||||
|
||||
static void
|
||||
nfc_debug_pcap_write(NfcDebugPcapWorker* instance, uint8_t event, uint8_t* data, uint16_t len) {
|
||||
FuriHalRtcDateTime datetime;
|
||||
furi_hal_rtc_get_datetime(&datetime);
|
||||
|
||||
struct {
|
||||
// https://wiki.wireshark.org/Development/LibpcapFileFormat#record-packet-header
|
||||
uint32_t ts_sec;
|
||||
uint32_t ts_usec;
|
||||
uint32_t incl_len;
|
||||
uint32_t orig_len;
|
||||
// https://www.kaiser.cx/posts/pcap-iso14443/#_packet_data
|
||||
uint8_t version;
|
||||
uint8_t event;
|
||||
uint16_t len;
|
||||
} __attribute__((__packed__)) pkt_hdr = {
|
||||
.ts_sec = furi_hal_rtc_datetime_to_timestamp(&datetime),
|
||||
.ts_usec = 0,
|
||||
.incl_len = len + 4,
|
||||
.orig_len = len + 4,
|
||||
.version = 0,
|
||||
.event = event,
|
||||
.len = len << 8 | len >> 8,
|
||||
};
|
||||
xStreamBufferSend(instance->stream, &pkt_hdr, sizeof(pkt_hdr), FuriWaitForever);
|
||||
xStreamBufferSend(instance->stream, data, len, FuriWaitForever);
|
||||
}
|
||||
|
||||
static void
|
||||
nfc_debug_pcap_write_tx(uint8_t* data, uint16_t bits, bool crc_dropped, void* context) {
|
||||
NfcDebugPcapWorker* instance = context;
|
||||
uint8_t event = crc_dropped ? DATA_PCD_TO_PICC_CRC_DROPPED : DATA_PCD_TO_PICC;
|
||||
nfc_debug_pcap_write(instance, event, data, bits / 8);
|
||||
}
|
||||
|
||||
static void
|
||||
nfc_debug_pcap_write_rx(uint8_t* data, uint16_t bits, bool crc_dropped, void* context) {
|
||||
NfcDebugPcapWorker* instance = context;
|
||||
uint8_t event = crc_dropped ? DATA_PICC_TO_PCD_CRC_DROPPED : DATA_PICC_TO_PCD;
|
||||
nfc_debug_pcap_write(instance, event, data, bits / 8);
|
||||
}
|
||||
|
||||
int32_t nfc_debug_pcap_thread(void* context) {
|
||||
NfcDebugPcapWorker* instance = context;
|
||||
uint8_t buffer[NFC_DEBUG_PCAP_BUFFER_SIZE];
|
||||
|
||||
while(instance->alive) {
|
||||
size_t ret =
|
||||
xStreamBufferReceive(instance->stream, buffer, NFC_DEBUG_PCAP_BUFFER_SIZE, 50);
|
||||
if(storage_file_write(instance->file, buffer, ret) != ret) {
|
||||
FURI_LOG_E(TAG, "Failed to write pcap data");
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
NfcDebugPcapWorker* nfc_debug_pcap_alloc(Storage* storage) {
|
||||
NfcDebugPcapWorker* instance = malloc(sizeof(NfcDebugPcapWorker));
|
||||
|
||||
instance->alive = true;
|
||||
|
||||
instance->storage = storage;
|
||||
|
||||
instance->file = nfc_debug_pcap_open(storage);
|
||||
|
||||
instance->stream = xStreamBufferCreate(4096, 1);
|
||||
|
||||
instance->thread = furi_thread_alloc();
|
||||
furi_thread_set_name(instance->thread, "PcapWorker");
|
||||
furi_thread_set_stack_size(instance->thread, 1024);
|
||||
furi_thread_set_callback(instance->thread, nfc_debug_pcap_thread);
|
||||
furi_thread_set_context(instance->thread, instance);
|
||||
furi_thread_start(instance->thread);
|
||||
|
||||
return instance;
|
||||
}
|
||||
|
||||
void nfc_debug_pcap_free(NfcDebugPcapWorker* instance) {
|
||||
furi_assert(instance);
|
||||
|
||||
instance->alive = false;
|
||||
|
||||
furi_thread_join(instance->thread);
|
||||
furi_thread_free(instance->thread);
|
||||
|
||||
vStreamBufferDelete(instance->stream);
|
||||
|
||||
if(instance->file) storage_file_free(instance->file);
|
||||
|
||||
instance->storage = NULL;
|
||||
|
||||
free(instance);
|
||||
}
|
||||
|
||||
void nfc_debug_pcap_prepare_tx_rx(
|
||||
NfcDebugPcapWorker* instance,
|
||||
FuriHalNfcTxRxContext* tx_rx,
|
||||
bool is_picc) {
|
||||
if(!instance || !instance->file) return;
|
||||
|
||||
if(is_picc) {
|
||||
tx_rx->sniff_tx = nfc_debug_pcap_write_rx;
|
||||
tx_rx->sniff_rx = nfc_debug_pcap_write_tx;
|
||||
} else {
|
||||
tx_rx->sniff_tx = nfc_debug_pcap_write_tx;
|
||||
tx_rx->sniff_rx = nfc_debug_pcap_write_rx;
|
||||
}
|
||||
|
||||
tx_rx->sniff_context = instance;
|
||||
}
|
||||
21
lib/nfc/helpers/nfc_debug_pcap.h
Normal file
21
lib/nfc/helpers/nfc_debug_pcap.h
Normal file
@@ -0,0 +1,21 @@
|
||||
#pragma once
|
||||
|
||||
#include <furi_hal_nfc.h>
|
||||
#include <storage/storage.h>
|
||||
|
||||
typedef struct NfcDebugPcapWorker NfcDebugPcapWorker;
|
||||
|
||||
NfcDebugPcapWorker* nfc_debug_pcap_alloc(Storage* storage);
|
||||
|
||||
void nfc_debug_pcap_free(NfcDebugPcapWorker* instance);
|
||||
|
||||
/** Prepare tx/rx context for debug pcap logging, if enabled.
|
||||
*
|
||||
* @param instance NfcDebugPcapWorker* instance, can be NULL
|
||||
* @param tx_rx TX/RX context to log
|
||||
* @param is_picc if true, record Flipper as PICC, else PCD.
|
||||
*/
|
||||
void nfc_debug_pcap_prepare_tx_rx(
|
||||
NfcDebugPcapWorker* instance,
|
||||
FuriHalNfcTxRxContext* tx_rx,
|
||||
bool is_picc);
|
||||
1278
lib/nfc/nfc_device.c
Normal file
1278
lib/nfc/nfc_device.c
Normal file
File diff suppressed because it is too large
Load Diff
94
lib/nfc/nfc_device.h
Normal file
94
lib/nfc/nfc_device.h
Normal file
@@ -0,0 +1,94 @@
|
||||
#pragma once
|
||||
|
||||
#include <stdint.h>
|
||||
#include <stdbool.h>
|
||||
#include <storage/storage.h>
|
||||
#include <dialogs/dialogs.h>
|
||||
|
||||
#include <furi_hal_nfc.h>
|
||||
#include <lib/nfc/protocols/emv.h>
|
||||
#include <lib/nfc/protocols/mifare_ultralight.h>
|
||||
#include <lib/nfc/protocols/mifare_classic.h>
|
||||
#include <lib/nfc/protocols/mifare_desfire.h>
|
||||
|
||||
#define NFC_DEV_NAME_MAX_LEN 22
|
||||
#define NFC_READER_DATA_MAX_SIZE 64
|
||||
|
||||
#define NFC_APP_FOLDER ANY_PATH("nfc")
|
||||
#define NFC_APP_EXTENSION ".nfc"
|
||||
#define NFC_APP_SHADOW_EXTENSION ".shd"
|
||||
|
||||
typedef void (*NfcLoadingCallback)(void* context, bool state);
|
||||
|
||||
typedef enum {
|
||||
NfcDeviceProtocolUnknown,
|
||||
NfcDeviceProtocolEMV,
|
||||
NfcDeviceProtocolMifareUl,
|
||||
NfcDeviceProtocolMifareClassic,
|
||||
NfcDeviceProtocolMifareDesfire,
|
||||
} NfcProtocol;
|
||||
|
||||
typedef enum {
|
||||
NfcDeviceSaveFormatUid,
|
||||
NfcDeviceSaveFormatBankCard,
|
||||
NfcDeviceSaveFormatMifareUl,
|
||||
NfcDeviceSaveFormatMifareClassic,
|
||||
NfcDeviceSaveFormatMifareDesfire,
|
||||
} NfcDeviceSaveFormat;
|
||||
|
||||
typedef struct {
|
||||
uint8_t data[NFC_READER_DATA_MAX_SIZE];
|
||||
uint16_t size;
|
||||
} NfcReaderRequestData;
|
||||
|
||||
typedef struct {
|
||||
FuriHalNfcDevData nfc_data;
|
||||
NfcProtocol protocol;
|
||||
NfcReaderRequestData reader_data;
|
||||
union {
|
||||
EmvData emv_data;
|
||||
MfUltralightData mf_ul_data;
|
||||
MfClassicData mf_classic_data;
|
||||
MifareDesfireData mf_df_data;
|
||||
};
|
||||
string_t parsed_data;
|
||||
} NfcDeviceData;
|
||||
|
||||
typedef struct {
|
||||
Storage* storage;
|
||||
DialogsApp* dialogs;
|
||||
NfcDeviceData dev_data;
|
||||
char dev_name[NFC_DEV_NAME_MAX_LEN + 1];
|
||||
string_t load_path;
|
||||
NfcDeviceSaveFormat format;
|
||||
bool shadow_file_exist;
|
||||
|
||||
NfcLoadingCallback loading_cb;
|
||||
void* loading_cb_ctx;
|
||||
} NfcDevice;
|
||||
|
||||
NfcDevice* nfc_device_alloc();
|
||||
|
||||
void nfc_device_free(NfcDevice* nfc_dev);
|
||||
|
||||
void nfc_device_set_name(NfcDevice* dev, const char* name);
|
||||
|
||||
bool nfc_device_save(NfcDevice* dev, const char* dev_name);
|
||||
|
||||
bool nfc_device_save_shadow(NfcDevice* dev, const char* dev_name);
|
||||
|
||||
bool nfc_device_load(NfcDevice* dev, const char* file_path, bool show_dialog);
|
||||
|
||||
bool nfc_device_load_key_cache(NfcDevice* dev);
|
||||
|
||||
bool nfc_file_select(NfcDevice* dev);
|
||||
|
||||
void nfc_device_data_clear(NfcDeviceData* dev);
|
||||
|
||||
void nfc_device_clear(NfcDevice* dev);
|
||||
|
||||
bool nfc_device_delete(NfcDevice* dev, bool use_load_path);
|
||||
|
||||
bool nfc_device_restore(NfcDevice* dev, bool use_load_path);
|
||||
|
||||
void nfc_device_set_loading_callback(NfcDevice* dev, NfcLoadingCallback callback, void* context);
|
||||
65
lib/nfc/nfc_types.c
Normal file
65
lib/nfc/nfc_types.c
Normal file
@@ -0,0 +1,65 @@
|
||||
#include "nfc_types.h"
|
||||
|
||||
const char* nfc_get_dev_type(FuriHalNfcType type) {
|
||||
if(type == FuriHalNfcTypeA) {
|
||||
return "NFC-A";
|
||||
} else if(type == FuriHalNfcTypeB) {
|
||||
return "NFC-B";
|
||||
} else if(type == FuriHalNfcTypeF) {
|
||||
return "NFC-F";
|
||||
} else if(type == FuriHalNfcTypeV) {
|
||||
return "NFC-V";
|
||||
} else {
|
||||
return "Unknown";
|
||||
}
|
||||
}
|
||||
|
||||
const char* nfc_guess_protocol(NfcProtocol protocol) {
|
||||
if(protocol == NfcDeviceProtocolEMV) {
|
||||
return "EMV bank card";
|
||||
} else if(protocol == NfcDeviceProtocolMifareUl) {
|
||||
return "Mifare Ultral/NTAG";
|
||||
} else if(protocol == NfcDeviceProtocolMifareClassic) {
|
||||
return "Mifare Classic";
|
||||
} else if(protocol == NfcDeviceProtocolMifareDesfire) {
|
||||
return "Mifare DESFire";
|
||||
} else {
|
||||
return "Unrecognized";
|
||||
}
|
||||
}
|
||||
|
||||
const char* nfc_mf_ul_type(MfUltralightType type, bool full_name) {
|
||||
if(type == MfUltralightTypeNTAG213) {
|
||||
return "NTAG213";
|
||||
} else if(type == MfUltralightTypeNTAG215) {
|
||||
return "NTAG215";
|
||||
} else if(type == MfUltralightTypeNTAG216) {
|
||||
return "NTAG216";
|
||||
} else if(type == MfUltralightTypeNTAGI2C1K) {
|
||||
return "NTAG I2C 1K";
|
||||
} else if(type == MfUltralightTypeNTAGI2C2K) {
|
||||
return "NTAG I2C 2K";
|
||||
} else if(type == MfUltralightTypeNTAGI2CPlus1K) {
|
||||
return "NTAG I2C Plus 1K";
|
||||
} else if(type == MfUltralightTypeNTAGI2CPlus2K) {
|
||||
return "NTAG I2C Plus 2K";
|
||||
} else if(type == MfUltralightTypeNTAG203) {
|
||||
return "NTAG203";
|
||||
} else if(type == MfUltralightTypeUL11 && full_name) {
|
||||
return "Mifare Ultralight 11";
|
||||
} else if(type == MfUltralightTypeUL21 && full_name) {
|
||||
return "Mifare Ultralight 21";
|
||||
} else {
|
||||
return "Mifare Ultralight";
|
||||
}
|
||||
}
|
||||
|
||||
const char* nfc_mf_classic_type(MfClassicType type) {
|
||||
if(type == MfClassicType1k) {
|
||||
return "Mifare Classic 1K";
|
||||
} else if(type == MfClassicType4k) {
|
||||
return "Mifare Classic 4K";
|
||||
} else {
|
||||
return "Mifare Classic";
|
||||
}
|
||||
}
|
||||
11
lib/nfc/nfc_types.h
Normal file
11
lib/nfc/nfc_types.h
Normal file
@@ -0,0 +1,11 @@
|
||||
#pragma once
|
||||
|
||||
#include "nfc_device.h"
|
||||
|
||||
const char* nfc_get_dev_type(FuriHalNfcType type);
|
||||
|
||||
const char* nfc_guess_protocol(NfcProtocol protocol);
|
||||
|
||||
const char* nfc_mf_ul_type(MfUltralightType type, bool full_name);
|
||||
|
||||
const char* nfc_mf_classic_type(MfClassicType type);
|
||||
510
lib/nfc/nfc_worker.c
Normal file
510
lib/nfc/nfc_worker.c
Normal file
@@ -0,0 +1,510 @@
|
||||
#include "nfc_worker_i.h"
|
||||
#include <furi_hal.h>
|
||||
|
||||
#include <platform.h>
|
||||
#include "parsers/nfc_supported_card.h"
|
||||
|
||||
#define TAG "NfcWorker"
|
||||
|
||||
/***************************** NFC Worker API *******************************/
|
||||
|
||||
NfcWorker* nfc_worker_alloc() {
|
||||
NfcWorker* nfc_worker = malloc(sizeof(NfcWorker));
|
||||
|
||||
// Worker thread attributes
|
||||
nfc_worker->thread = furi_thread_alloc();
|
||||
furi_thread_set_name(nfc_worker->thread, "NfcWorker");
|
||||
furi_thread_set_stack_size(nfc_worker->thread, 8192);
|
||||
furi_thread_set_callback(nfc_worker->thread, nfc_worker_task);
|
||||
furi_thread_set_context(nfc_worker->thread, nfc_worker);
|
||||
|
||||
nfc_worker->callback = NULL;
|
||||
nfc_worker->context = NULL;
|
||||
nfc_worker->storage = furi_record_open(RECORD_STORAGE);
|
||||
|
||||
// Initialize rfal
|
||||
while(furi_hal_nfc_is_busy()) {
|
||||
furi_delay_ms(10);
|
||||
}
|
||||
nfc_worker_change_state(nfc_worker, NfcWorkerStateReady);
|
||||
|
||||
if(furi_hal_rtc_is_flag_set(FuriHalRtcFlagDebug)) {
|
||||
nfc_worker->debug_pcap_worker = nfc_debug_pcap_alloc(nfc_worker->storage);
|
||||
}
|
||||
|
||||
return nfc_worker;
|
||||
}
|
||||
|
||||
void nfc_worker_free(NfcWorker* nfc_worker) {
|
||||
furi_assert(nfc_worker);
|
||||
|
||||
furi_thread_free(nfc_worker->thread);
|
||||
|
||||
furi_record_close(RECORD_STORAGE);
|
||||
|
||||
if(nfc_worker->debug_pcap_worker) nfc_debug_pcap_free(nfc_worker->debug_pcap_worker);
|
||||
|
||||
free(nfc_worker);
|
||||
}
|
||||
|
||||
NfcWorkerState nfc_worker_get_state(NfcWorker* nfc_worker) {
|
||||
return nfc_worker->state;
|
||||
}
|
||||
|
||||
void nfc_worker_start(
|
||||
NfcWorker* nfc_worker,
|
||||
NfcWorkerState state,
|
||||
NfcDeviceData* dev_data,
|
||||
NfcWorkerCallback callback,
|
||||
void* context) {
|
||||
furi_assert(nfc_worker);
|
||||
furi_assert(dev_data);
|
||||
while(furi_hal_nfc_is_busy()) {
|
||||
furi_delay_ms(10);
|
||||
}
|
||||
|
||||
nfc_worker->callback = callback;
|
||||
nfc_worker->context = context;
|
||||
nfc_worker->dev_data = dev_data;
|
||||
nfc_worker_change_state(nfc_worker, state);
|
||||
furi_thread_start(nfc_worker->thread);
|
||||
}
|
||||
|
||||
void nfc_worker_stop(NfcWorker* nfc_worker) {
|
||||
furi_assert(nfc_worker);
|
||||
if(nfc_worker->state == NfcWorkerStateBroken || nfc_worker->state == NfcWorkerStateReady) {
|
||||
return;
|
||||
}
|
||||
furi_hal_nfc_stop();
|
||||
nfc_worker_change_state(nfc_worker, NfcWorkerStateStop);
|
||||
furi_thread_join(nfc_worker->thread);
|
||||
}
|
||||
|
||||
void nfc_worker_change_state(NfcWorker* nfc_worker, NfcWorkerState state) {
|
||||
nfc_worker->state = state;
|
||||
}
|
||||
|
||||
/***************************** NFC Worker Thread *******************************/
|
||||
|
||||
int32_t nfc_worker_task(void* context) {
|
||||
NfcWorker* nfc_worker = context;
|
||||
|
||||
furi_hal_nfc_exit_sleep();
|
||||
|
||||
if(nfc_worker->state == NfcWorkerStateRead) {
|
||||
nfc_worker_read(nfc_worker);
|
||||
} else if(nfc_worker->state == NfcWorkerStateUidEmulate) {
|
||||
nfc_worker_emulate_uid(nfc_worker);
|
||||
} else if(nfc_worker->state == NfcWorkerStateEmulateApdu) {
|
||||
nfc_worker_emulate_apdu(nfc_worker);
|
||||
} else if(nfc_worker->state == NfcWorkerStateMfUltralightEmulate) {
|
||||
nfc_worker_emulate_mf_ultralight(nfc_worker);
|
||||
} else if(nfc_worker->state == NfcWorkerStateMfClassicEmulate) {
|
||||
nfc_worker_emulate_mf_classic(nfc_worker);
|
||||
} else if(nfc_worker->state == NfcWorkerStateMfClassicUserDictAttack) {
|
||||
nfc_worker_mf_classic_dict_attack(nfc_worker, MfClassicDictTypeUser);
|
||||
} else if(nfc_worker->state == NfcWorkerStateMfClassicFlipperDictAttack) {
|
||||
nfc_worker_mf_classic_dict_attack(nfc_worker, MfClassicDictTypeFlipper);
|
||||
}
|
||||
furi_hal_nfc_sleep();
|
||||
nfc_worker_change_state(nfc_worker, NfcWorkerStateReady);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static bool nfc_worker_read_mf_ultralight(NfcWorker* nfc_worker, FuriHalNfcTxRxContext* tx_rx) {
|
||||
bool read_success = false;
|
||||
MfUltralightReader reader = {};
|
||||
MfUltralightData data = {};
|
||||
|
||||
nfc_debug_pcap_prepare_tx_rx(nfc_worker->debug_pcap_worker, tx_rx, false);
|
||||
do {
|
||||
// Read card
|
||||
if(!furi_hal_nfc_detect(&nfc_worker->dev_data->nfc_data, 200)) break;
|
||||
if(!mf_ul_read_card(tx_rx, &reader, &data)) break;
|
||||
// Copy data
|
||||
nfc_worker->dev_data->mf_ul_data = data;
|
||||
read_success = true;
|
||||
} while(false);
|
||||
|
||||
return read_success;
|
||||
}
|
||||
|
||||
static bool nfc_worker_read_mf_classic(NfcWorker* nfc_worker, FuriHalNfcTxRxContext* tx_rx) {
|
||||
furi_assert(nfc_worker->callback);
|
||||
bool read_success = false;
|
||||
|
||||
nfc_debug_pcap_prepare_tx_rx(nfc_worker->debug_pcap_worker, tx_rx, false);
|
||||
do {
|
||||
// Try to read supported card
|
||||
FURI_LOG_I(TAG, "Try read supported card ...");
|
||||
for(size_t i = 0; i < NfcSupportedCardTypeEnd; i++) {
|
||||
if(nfc_supported_card[i].protocol == NfcDeviceProtocolMifareClassic) {
|
||||
if(nfc_supported_card[i].verify(nfc_worker, tx_rx)) {
|
||||
if(nfc_supported_card[i].read(nfc_worker, tx_rx)) {
|
||||
read_success = true;
|
||||
nfc_supported_card[i].parse(nfc_worker);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if(read_success) break;
|
||||
// Try to read card with key cache
|
||||
FURI_LOG_I(TAG, "Search for key cache ...");
|
||||
if(nfc_worker->callback(NfcWorkerEventReadMfClassicLoadKeyCache, nfc_worker->context)) {
|
||||
FURI_LOG_I(TAG, "Load keys cache success. Start reading");
|
||||
uint8_t sectors_read =
|
||||
mf_classic_update_card(tx_rx, &nfc_worker->dev_data->mf_classic_data);
|
||||
uint8_t sectors_total =
|
||||
mf_classic_get_total_sectors_num(nfc_worker->dev_data->mf_classic_data.type);
|
||||
FURI_LOG_I(TAG, "Read %d sectors out of %d total", sectors_read, sectors_total);
|
||||
read_success = (sectors_read == sectors_total);
|
||||
}
|
||||
} while(false);
|
||||
|
||||
return read_success;
|
||||
}
|
||||
|
||||
static bool nfc_worker_read_mf_desfire(NfcWorker* nfc_worker, FuriHalNfcTxRxContext* tx_rx) {
|
||||
bool read_success = false;
|
||||
MifareDesfireData* data = &nfc_worker->dev_data->mf_df_data;
|
||||
|
||||
nfc_debug_pcap_prepare_tx_rx(nfc_worker->debug_pcap_worker, tx_rx, false);
|
||||
do {
|
||||
if(!furi_hal_nfc_detect(&nfc_worker->dev_data->nfc_data, 300)) break;
|
||||
if(!mf_df_read_card(tx_rx, data)) break;
|
||||
read_success = true;
|
||||
} while(false);
|
||||
|
||||
return read_success;
|
||||
}
|
||||
|
||||
static bool nfc_worker_read_bank_card(NfcWorker* nfc_worker, FuriHalNfcTxRxContext* tx_rx) {
|
||||
bool read_success = false;
|
||||
EmvApplication emv_app = {};
|
||||
EmvData* result = &nfc_worker->dev_data->emv_data;
|
||||
|
||||
nfc_debug_pcap_prepare_tx_rx(nfc_worker->debug_pcap_worker, tx_rx, false);
|
||||
do {
|
||||
// Read card
|
||||
if(!furi_hal_nfc_detect(&nfc_worker->dev_data->nfc_data, 300)) break;
|
||||
if(!emv_read_bank_card(tx_rx, &emv_app)) break;
|
||||
// Copy data
|
||||
// TODO Set EmvData to reader or like in mifare ultralight!
|
||||
result->number_len = emv_app.card_number_len;
|
||||
memcpy(result->number, emv_app.card_number, result->number_len);
|
||||
result->aid_len = emv_app.aid_len;
|
||||
memcpy(result->aid, emv_app.aid, result->aid_len);
|
||||
if(emv_app.name_found) {
|
||||
memcpy(result->name, emv_app.name, sizeof(emv_app.name));
|
||||
}
|
||||
if(emv_app.exp_month) {
|
||||
result->exp_mon = emv_app.exp_month;
|
||||
result->exp_year = emv_app.exp_year;
|
||||
}
|
||||
if(emv_app.country_code) {
|
||||
result->country_code = emv_app.country_code;
|
||||
}
|
||||
if(emv_app.currency_code) {
|
||||
result->currency_code = emv_app.currency_code;
|
||||
}
|
||||
read_success = true;
|
||||
} while(false);
|
||||
|
||||
return read_success;
|
||||
}
|
||||
|
||||
static bool nfc_worker_read_nfca(NfcWorker* nfc_worker, FuriHalNfcTxRxContext* tx_rx) {
|
||||
FuriHalNfcDevData* nfc_data = &nfc_worker->dev_data->nfc_data;
|
||||
|
||||
bool card_read = false;
|
||||
furi_hal_nfc_sleep();
|
||||
if(mf_ul_check_card_type(nfc_data->atqa[0], nfc_data->atqa[1], nfc_data->sak)) {
|
||||
FURI_LOG_I(TAG, "Mifare Ultralight / NTAG detected");
|
||||
nfc_worker->dev_data->protocol = NfcDeviceProtocolMifareUl;
|
||||
card_read = nfc_worker_read_mf_ultralight(nfc_worker, tx_rx);
|
||||
} else if(mf_classic_check_card_type(nfc_data->atqa[0], nfc_data->atqa[1], nfc_data->sak)) {
|
||||
FURI_LOG_I(TAG, "Mifare Classic detected");
|
||||
nfc_worker->dev_data->protocol = NfcDeviceProtocolMifareClassic;
|
||||
nfc_worker->dev_data->mf_classic_data.type =
|
||||
mf_classic_get_classic_type(nfc_data->atqa[0], nfc_data->atqa[1], nfc_data->sak);
|
||||
card_read = nfc_worker_read_mf_classic(nfc_worker, tx_rx);
|
||||
} else if(mf_df_check_card_type(nfc_data->atqa[0], nfc_data->atqa[1], nfc_data->sak)) {
|
||||
FURI_LOG_I(TAG, "Mifare DESFire detected");
|
||||
nfc_worker->dev_data->protocol = NfcDeviceProtocolMifareDesfire;
|
||||
if(!nfc_worker_read_mf_desfire(nfc_worker, tx_rx)) {
|
||||
FURI_LOG_I(TAG, "Unknown card. Save UID");
|
||||
nfc_worker->dev_data->protocol = NfcDeviceProtocolUnknown;
|
||||
}
|
||||
card_read = true;
|
||||
} else if(nfc_data->interface == FuriHalNfcInterfaceIsoDep) {
|
||||
FURI_LOG_I(TAG, "ISO14443-4 card detected");
|
||||
nfc_worker->dev_data->protocol = NfcDeviceProtocolEMV;
|
||||
if(!nfc_worker_read_bank_card(nfc_worker, tx_rx)) {
|
||||
FURI_LOG_I(TAG, "Unknown card. Save UID");
|
||||
nfc_worker->dev_data->protocol = NfcDeviceProtocolUnknown;
|
||||
}
|
||||
card_read = true;
|
||||
}
|
||||
|
||||
return card_read;
|
||||
}
|
||||
|
||||
void nfc_worker_read(NfcWorker* nfc_worker) {
|
||||
furi_assert(nfc_worker);
|
||||
furi_assert(nfc_worker->callback);
|
||||
|
||||
nfc_device_data_clear(nfc_worker->dev_data);
|
||||
NfcDeviceData* dev_data = nfc_worker->dev_data;
|
||||
FuriHalNfcDevData* nfc_data = &nfc_worker->dev_data->nfc_data;
|
||||
FuriHalNfcTxRxContext tx_rx = {};
|
||||
NfcWorkerEvent event = 0;
|
||||
bool card_not_detected_notified = false;
|
||||
|
||||
while(nfc_worker->state == NfcWorkerStateRead) {
|
||||
if(furi_hal_nfc_detect(nfc_data, 300)) {
|
||||
// Process first found device
|
||||
nfc_worker->callback(NfcWorkerEventCardDetected, nfc_worker->context);
|
||||
card_not_detected_notified = false;
|
||||
if(nfc_data->type == FuriHalNfcTypeA) {
|
||||
if(nfc_worker_read_nfca(nfc_worker, &tx_rx)) {
|
||||
if(dev_data->protocol == NfcDeviceProtocolMifareUl) {
|
||||
event = NfcWorkerEventReadMfUltralight;
|
||||
break;
|
||||
} else if(dev_data->protocol == NfcDeviceProtocolMifareClassic) {
|
||||
event = NfcWorkerEventReadMfClassicDone;
|
||||
break;
|
||||
} else if(dev_data->protocol == NfcDeviceProtocolMifareDesfire) {
|
||||
event = NfcWorkerEventReadMfDesfire;
|
||||
break;
|
||||
} else if(dev_data->protocol == NfcDeviceProtocolEMV) {
|
||||
event = NfcWorkerEventReadBankCard;
|
||||
break;
|
||||
} else if(dev_data->protocol == NfcDeviceProtocolUnknown) {
|
||||
event = NfcWorkerEventReadUidNfcA;
|
||||
break;
|
||||
}
|
||||
} else {
|
||||
if(dev_data->protocol == NfcDeviceProtocolMifareClassic) {
|
||||
event = NfcWorkerEventReadMfClassicDictAttackRequired;
|
||||
break;
|
||||
}
|
||||
}
|
||||
} else if(nfc_data->type == FuriHalNfcTypeB) {
|
||||
event = NfcWorkerEventReadUidNfcB;
|
||||
break;
|
||||
} else if(nfc_data->type == FuriHalNfcTypeF) {
|
||||
event = NfcWorkerEventReadUidNfcF;
|
||||
break;
|
||||
} else if(nfc_data->type == FuriHalNfcTypeV) {
|
||||
event = NfcWorkerEventReadUidNfcV;
|
||||
break;
|
||||
}
|
||||
} else {
|
||||
if(!card_not_detected_notified) {
|
||||
nfc_worker->callback(NfcWorkerEventNoCardDetected, nfc_worker->context);
|
||||
card_not_detected_notified = true;
|
||||
}
|
||||
}
|
||||
furi_hal_nfc_sleep();
|
||||
furi_delay_ms(100);
|
||||
}
|
||||
// Notify caller and exit
|
||||
if(event > NfcWorkerEventReserved) {
|
||||
nfc_worker->callback(event, nfc_worker->context);
|
||||
}
|
||||
}
|
||||
|
||||
void nfc_worker_emulate_uid(NfcWorker* nfc_worker) {
|
||||
FuriHalNfcTxRxContext tx_rx = {};
|
||||
nfc_debug_pcap_prepare_tx_rx(nfc_worker->debug_pcap_worker, &tx_rx, true);
|
||||
FuriHalNfcDevData* data = &nfc_worker->dev_data->nfc_data;
|
||||
NfcReaderRequestData* reader_data = &nfc_worker->dev_data->reader_data;
|
||||
|
||||
while(nfc_worker->state == NfcWorkerStateUidEmulate) {
|
||||
if(furi_hal_nfc_listen(data->uid, data->uid_len, data->atqa, data->sak, true, 100)) {
|
||||
if(furi_hal_nfc_tx_rx(&tx_rx, 100)) {
|
||||
reader_data->size = tx_rx.rx_bits / 8;
|
||||
if(reader_data->size > 0) {
|
||||
memcpy(reader_data->data, tx_rx.rx_data, reader_data->size);
|
||||
if(nfc_worker->callback) {
|
||||
nfc_worker->callback(NfcWorkerEventSuccess, nfc_worker->context);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
FURI_LOG_E(TAG, "Failed to get reader commands");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void nfc_worker_emulate_apdu(NfcWorker* nfc_worker) {
|
||||
FuriHalNfcTxRxContext tx_rx = {};
|
||||
nfc_debug_pcap_prepare_tx_rx(nfc_worker->debug_pcap_worker, &tx_rx, true);
|
||||
FuriHalNfcDevData params = {
|
||||
.uid = {0xCF, 0x72, 0xd4, 0x40},
|
||||
.uid_len = 4,
|
||||
.atqa = {0x00, 0x04},
|
||||
.sak = 0x20,
|
||||
.type = FuriHalNfcTypeA,
|
||||
};
|
||||
|
||||
while(nfc_worker->state == NfcWorkerStateEmulateApdu) {
|
||||
if(furi_hal_nfc_listen(params.uid, params.uid_len, params.atqa, params.sak, false, 300)) {
|
||||
FURI_LOG_D(TAG, "POS terminal detected");
|
||||
if(emv_card_emulation(&tx_rx)) {
|
||||
FURI_LOG_D(TAG, "EMV card emulated");
|
||||
}
|
||||
} else {
|
||||
FURI_LOG_D(TAG, "Can't find reader");
|
||||
}
|
||||
furi_hal_nfc_sleep();
|
||||
furi_delay_ms(20);
|
||||
}
|
||||
}
|
||||
|
||||
void nfc_worker_emulate_mf_ultralight(NfcWorker* nfc_worker) {
|
||||
FuriHalNfcDevData* nfc_data = &nfc_worker->dev_data->nfc_data;
|
||||
MfUltralightEmulator emulator = {};
|
||||
mf_ul_prepare_emulation(&emulator, &nfc_worker->dev_data->mf_ul_data);
|
||||
while(nfc_worker->state == NfcWorkerStateMfUltralightEmulate) {
|
||||
mf_ul_reset_emulation(&emulator, true);
|
||||
furi_hal_nfc_emulate_nfca(
|
||||
nfc_data->uid,
|
||||
nfc_data->uid_len,
|
||||
nfc_data->atqa,
|
||||
nfc_data->sak,
|
||||
mf_ul_prepare_emulation_response,
|
||||
&emulator,
|
||||
5000);
|
||||
// Check if data was modified
|
||||
if(emulator.data_changed) {
|
||||
nfc_worker->dev_data->mf_ul_data = emulator.data;
|
||||
if(nfc_worker->callback) {
|
||||
nfc_worker->callback(NfcWorkerEventSuccess, nfc_worker->context);
|
||||
}
|
||||
emulator.data_changed = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void nfc_worker_mf_classic_dict_attack(NfcWorker* nfc_worker, MfClassicDictType type) {
|
||||
furi_assert(nfc_worker);
|
||||
furi_assert(nfc_worker->callback);
|
||||
|
||||
MfClassicData* data = &nfc_worker->dev_data->mf_classic_data;
|
||||
uint32_t total_sectors = mf_classic_get_total_sectors_num(data->type);
|
||||
uint64_t key = 0;
|
||||
FuriHalNfcTxRxContext tx_rx = {};
|
||||
bool card_found_notified = true;
|
||||
bool card_removed_notified = false;
|
||||
|
||||
// Load dictionary
|
||||
MfClassicDict* dict = mf_classic_dict_alloc(type);
|
||||
if(!dict) {
|
||||
FURI_LOG_E(TAG, "Dictionary not found");
|
||||
nfc_worker->callback(NfcWorkerEventNoDictFound, nfc_worker->context);
|
||||
mf_classic_dict_free(dict);
|
||||
return;
|
||||
}
|
||||
|
||||
FURI_LOG_D(TAG, "Start Dictionary attack");
|
||||
for(size_t i = 0; i < total_sectors; i++) {
|
||||
FURI_LOG_I(TAG, "Sector %d", i);
|
||||
nfc_worker->callback(NfcWorkerEventNewSector, nfc_worker->context);
|
||||
uint8_t block_num = mf_classic_get_sector_trailer_block_num_by_sector(i);
|
||||
if(mf_classic_is_sector_read(data, i)) continue;
|
||||
bool is_key_a_found = mf_classic_is_key_found(data, i, MfClassicKeyA);
|
||||
bool is_key_b_found = mf_classic_is_key_found(data, i, MfClassicKeyB);
|
||||
while(mf_classic_dict_get_next_key(dict, &key)) {
|
||||
furi_hal_nfc_sleep();
|
||||
if(furi_hal_nfc_activate_nfca(200, NULL)) {
|
||||
furi_hal_nfc_sleep();
|
||||
if(!card_found_notified) {
|
||||
nfc_worker->callback(NfcWorkerEventCardDetected, nfc_worker->context);
|
||||
card_found_notified = true;
|
||||
card_removed_notified = false;
|
||||
}
|
||||
FURI_LOG_D(
|
||||
TAG,
|
||||
"Try to auth to sector %d with key %04lx%08lx",
|
||||
i,
|
||||
(uint32_t)(key >> 32),
|
||||
(uint32_t)key);
|
||||
if(!is_key_a_found) {
|
||||
is_key_a_found = mf_classic_is_key_found(data, i, MfClassicKeyA);
|
||||
if(mf_classic_authenticate(&tx_rx, block_num, key, MfClassicKeyA)) {
|
||||
mf_classic_set_key_found(data, i, MfClassicKeyA, key);
|
||||
nfc_worker->callback(NfcWorkerEventFoundKeyA, nfc_worker->context);
|
||||
}
|
||||
furi_hal_nfc_sleep();
|
||||
}
|
||||
if(!is_key_b_found) {
|
||||
is_key_b_found = mf_classic_is_key_found(data, i, MfClassicKeyB);
|
||||
if(mf_classic_authenticate(&tx_rx, block_num, key, MfClassicKeyB)) {
|
||||
mf_classic_set_key_found(data, i, MfClassicKeyB, key);
|
||||
nfc_worker->callback(NfcWorkerEventFoundKeyB, nfc_worker->context);
|
||||
}
|
||||
}
|
||||
if(is_key_a_found && is_key_b_found) break;
|
||||
if(!((nfc_worker->state == NfcWorkerStateMfClassicUserDictAttack) ||
|
||||
(nfc_worker->state == NfcWorkerStateMfClassicFlipperDictAttack)))
|
||||
break;
|
||||
} else {
|
||||
if(!card_removed_notified) {
|
||||
nfc_worker->callback(NfcWorkerEventNoCardDetected, nfc_worker->context);
|
||||
card_removed_notified = true;
|
||||
card_found_notified = false;
|
||||
}
|
||||
if(!((nfc_worker->state == NfcWorkerStateMfClassicUserDictAttack) ||
|
||||
(nfc_worker->state == NfcWorkerStateMfClassicFlipperDictAttack)))
|
||||
break;
|
||||
}
|
||||
}
|
||||
if(!((nfc_worker->state == NfcWorkerStateMfClassicUserDictAttack) ||
|
||||
(nfc_worker->state == NfcWorkerStateMfClassicFlipperDictAttack)))
|
||||
break;
|
||||
mf_classic_read_sector(&tx_rx, data, i);
|
||||
mf_classic_dict_rewind(dict);
|
||||
}
|
||||
mf_classic_dict_free(dict);
|
||||
if((nfc_worker->state == NfcWorkerStateMfClassicUserDictAttack) ||
|
||||
(nfc_worker->state == NfcWorkerStateMfClassicFlipperDictAttack)) {
|
||||
nfc_worker->callback(NfcWorkerEventSuccess, nfc_worker->context);
|
||||
} else {
|
||||
nfc_worker->callback(NfcWorkerEventAborted, nfc_worker->context);
|
||||
}
|
||||
}
|
||||
|
||||
void nfc_worker_emulate_mf_classic(NfcWorker* nfc_worker) {
|
||||
FuriHalNfcTxRxContext tx_rx = {};
|
||||
nfc_debug_pcap_prepare_tx_rx(nfc_worker->debug_pcap_worker, &tx_rx, true);
|
||||
FuriHalNfcDevData* nfc_data = &nfc_worker->dev_data->nfc_data;
|
||||
MfClassicEmulator emulator = {
|
||||
.cuid = nfc_util_bytes2num(&nfc_data->uid[nfc_data->uid_len - 4], 4),
|
||||
.data = nfc_worker->dev_data->mf_classic_data,
|
||||
.data_changed = false,
|
||||
};
|
||||
NfcaSignal* nfca_signal = nfca_signal_alloc();
|
||||
tx_rx.nfca_signal = nfca_signal;
|
||||
|
||||
rfal_platform_spi_acquire();
|
||||
|
||||
furi_hal_nfc_listen_start(nfc_data);
|
||||
while(nfc_worker->state == NfcWorkerStateMfClassicEmulate) {
|
||||
if(furi_hal_nfc_listen_rx(&tx_rx, 300)) {
|
||||
mf_classic_emulator(&emulator, &tx_rx);
|
||||
}
|
||||
}
|
||||
if(emulator.data_changed) {
|
||||
nfc_worker->dev_data->mf_classic_data = emulator.data;
|
||||
if(nfc_worker->callback) {
|
||||
nfc_worker->callback(NfcWorkerEventSuccess, nfc_worker->context);
|
||||
}
|
||||
emulator.data_changed = false;
|
||||
}
|
||||
|
||||
nfca_signal_free(nfca_signal);
|
||||
|
||||
rfal_platform_spi_release();
|
||||
}
|
||||
71
lib/nfc/nfc_worker.h
Executable file
71
lib/nfc/nfc_worker.h
Executable file
@@ -0,0 +1,71 @@
|
||||
#pragma once
|
||||
|
||||
#include "nfc_device.h"
|
||||
|
||||
typedef struct NfcWorker NfcWorker;
|
||||
|
||||
typedef enum {
|
||||
// Init states
|
||||
NfcWorkerStateNone,
|
||||
NfcWorkerStateBroken,
|
||||
NfcWorkerStateReady,
|
||||
// Main worker states
|
||||
NfcWorkerStateRead,
|
||||
NfcWorkerStateUidEmulate,
|
||||
NfcWorkerStateMfUltralightEmulate,
|
||||
NfcWorkerStateMfClassicEmulate,
|
||||
NfcWorkerStateMfClassicUserDictAttack,
|
||||
NfcWorkerStateMfClassicFlipperDictAttack,
|
||||
// Debug
|
||||
NfcWorkerStateEmulateApdu,
|
||||
NfcWorkerStateField,
|
||||
// Transition
|
||||
NfcWorkerStateStop,
|
||||
} NfcWorkerState;
|
||||
|
||||
typedef enum {
|
||||
// Reserve first 50 events for application events
|
||||
NfcWorkerEventReserved = 50,
|
||||
|
||||
// Nfc read events
|
||||
NfcWorkerEventReadUidNfcB,
|
||||
NfcWorkerEventReadUidNfcV,
|
||||
NfcWorkerEventReadUidNfcF,
|
||||
NfcWorkerEventReadUidNfcA,
|
||||
NfcWorkerEventReadMfUltralight,
|
||||
NfcWorkerEventReadMfDesfire,
|
||||
NfcWorkerEventReadMfClassicDone,
|
||||
NfcWorkerEventReadMfClassicLoadKeyCache,
|
||||
NfcWorkerEventReadMfClassicDictAttackRequired,
|
||||
NfcWorkerEventReadBankCard,
|
||||
|
||||
// Nfc worker common events
|
||||
NfcWorkerEventSuccess,
|
||||
NfcWorkerEventFail,
|
||||
NfcWorkerEventAborted,
|
||||
NfcWorkerEventCardDetected,
|
||||
NfcWorkerEventNoCardDetected,
|
||||
|
||||
// Mifare Classic events
|
||||
NfcWorkerEventNoDictFound,
|
||||
NfcWorkerEventNewSector,
|
||||
NfcWorkerEventFoundKeyA,
|
||||
NfcWorkerEventFoundKeyB,
|
||||
} NfcWorkerEvent;
|
||||
|
||||
typedef bool (*NfcWorkerCallback)(NfcWorkerEvent event, void* context);
|
||||
|
||||
NfcWorker* nfc_worker_alloc();
|
||||
|
||||
NfcWorkerState nfc_worker_get_state(NfcWorker* nfc_worker);
|
||||
|
||||
void nfc_worker_free(NfcWorker* nfc_worker);
|
||||
|
||||
void nfc_worker_start(
|
||||
NfcWorker* nfc_worker,
|
||||
NfcWorkerState state,
|
||||
NfcDeviceData* dev_data,
|
||||
NfcWorkerCallback callback,
|
||||
void* context);
|
||||
|
||||
void nfc_worker_stop(NfcWorker* nfc_worker);
|
||||
48
lib/nfc/nfc_worker_i.h
Normal file
48
lib/nfc/nfc_worker_i.h
Normal file
@@ -0,0 +1,48 @@
|
||||
#pragma once
|
||||
|
||||
#include "nfc_worker.h"
|
||||
|
||||
#include <furi.h>
|
||||
#include <lib/toolbox/stream/file_stream.h>
|
||||
|
||||
#include <lib/nfc/protocols/nfc_util.h>
|
||||
#include <lib/nfc/protocols/emv.h>
|
||||
#include <lib/nfc/protocols/mifare_common.h>
|
||||
#include <lib/nfc/protocols/mifare_ultralight.h>
|
||||
#include <lib/nfc/protocols/mifare_classic.h>
|
||||
#include <lib/nfc/protocols/mifare_desfire.h>
|
||||
#include <lib/nfc/protocols/nfca.h>
|
||||
|
||||
#include "helpers/mf_classic_dict.h"
|
||||
#include "helpers/nfc_debug_pcap.h"
|
||||
|
||||
struct NfcWorker {
|
||||
FuriThread* thread;
|
||||
Storage* storage;
|
||||
Stream* dict_stream;
|
||||
|
||||
NfcDeviceData* dev_data;
|
||||
|
||||
NfcWorkerCallback callback;
|
||||
void* context;
|
||||
|
||||
NfcWorkerState state;
|
||||
|
||||
NfcDebugPcapWorker* debug_pcap_worker;
|
||||
};
|
||||
|
||||
void nfc_worker_change_state(NfcWorker* nfc_worker, NfcWorkerState state);
|
||||
|
||||
int32_t nfc_worker_task(void* context);
|
||||
|
||||
void nfc_worker_read(NfcWorker* nfc_worker);
|
||||
|
||||
void nfc_worker_emulate_uid(NfcWorker* nfc_worker);
|
||||
|
||||
void nfc_worker_emulate_mf_ultralight(NfcWorker* nfc_worker);
|
||||
|
||||
void nfc_worker_emulate_mf_classic(NfcWorker* nfc_worker);
|
||||
|
||||
void nfc_worker_mf_classic_dict_attack(NfcWorker* nfc_worker, MfClassicDictType type);
|
||||
|
||||
void nfc_worker_emulate_apdu(NfcWorker* nfc_worker);
|
||||
12
lib/nfc/parsers/nfc_supported_card.c
Normal file
12
lib/nfc/parsers/nfc_supported_card.c
Normal file
@@ -0,0 +1,12 @@
|
||||
#include "nfc_supported_card.h"
|
||||
|
||||
#include "troyka_parser.h"
|
||||
|
||||
NfcSupportedCard nfc_supported_card[NfcSupportedCardTypeEnd] = {
|
||||
[NfcSupportedCardTypeTroyka] = {
|
||||
.protocol = NfcDeviceProtocolMifareClassic,
|
||||
.verify = troyka_parser_verify,
|
||||
.read = troyka_parser_read,
|
||||
.parse = troyka_parser_parse,
|
||||
},
|
||||
};
|
||||
27
lib/nfc/parsers/nfc_supported_card.h
Normal file
27
lib/nfc/parsers/nfc_supported_card.h
Normal file
@@ -0,0 +1,27 @@
|
||||
#pragma once
|
||||
|
||||
#include <furi_hal_nfc.h>
|
||||
#include "../nfc_worker.h"
|
||||
|
||||
#include <m-string.h>
|
||||
|
||||
typedef enum {
|
||||
NfcSupportedCardTypeTroyka,
|
||||
|
||||
NfcSupportedCardTypeEnd,
|
||||
} NfcSupportedCardType;
|
||||
|
||||
typedef bool (*NfcSupportedCardVerify)(NfcWorker* nfc_worker, FuriHalNfcTxRxContext* tx_rx);
|
||||
|
||||
typedef bool (*NfcSupportedCardRead)(NfcWorker* nfc_worker, FuriHalNfcTxRxContext* tx_rx);
|
||||
|
||||
typedef bool (*NfcSupportedCardParse)(NfcWorker* nfc_worker);
|
||||
|
||||
typedef struct {
|
||||
NfcProtocol protocol;
|
||||
NfcSupportedCardVerify verify;
|
||||
NfcSupportedCardRead read;
|
||||
NfcSupportedCardParse parse;
|
||||
} NfcSupportedCard;
|
||||
|
||||
extern NfcSupportedCard nfc_supported_card[NfcSupportedCardTypeEnd];
|
||||
70
lib/nfc/parsers/troyka_parser.c
Normal file
70
lib/nfc/parsers/troyka_parser.c
Normal file
@@ -0,0 +1,70 @@
|
||||
#include "nfc_supported_card.h"
|
||||
|
||||
#include <gui/modules/widget.h>
|
||||
#include <nfc_worker_i.h>
|
||||
|
||||
static const MfClassicAuthContext troyka_keys[] = {
|
||||
{.sector = 0, .key_a = 0xa0a1a2a3a4a5, .key_b = 0xfbf225dc5d58},
|
||||
{.sector = 1, .key_a = 0xa82607b01c0d, .key_b = 0x2910989b6880},
|
||||
{.sector = 2, .key_a = 0x2aa05ed1856f, .key_b = 0xeaac88e5dc99},
|
||||
{.sector = 3, .key_a = 0x2aa05ed1856f, .key_b = 0xeaac88e5dc99},
|
||||
{.sector = 4, .key_a = 0x73068f118c13, .key_b = 0x2b7f3253fac5},
|
||||
{.sector = 5, .key_a = 0xfbc2793d540b, .key_b = 0xd3a297dc2698},
|
||||
{.sector = 6, .key_a = 0x2aa05ed1856f, .key_b = 0xeaac88e5dc99},
|
||||
{.sector = 7, .key_a = 0xae3d65a3dad4, .key_b = 0x0f1c63013dba},
|
||||
{.sector = 8, .key_a = 0xa73f5dc1d333, .key_b = 0xe35173494a81},
|
||||
{.sector = 9, .key_a = 0x69a32f1c2f19, .key_b = 0x6b8bd9860763},
|
||||
{.sector = 10, .key_a = 0x9becdf3d9273, .key_b = 0xf8493407799d},
|
||||
{.sector = 11, .key_a = 0x08b386463229, .key_b = 0x5efbaecef46b},
|
||||
{.sector = 12, .key_a = 0xcd4c61c26e3d, .key_b = 0x31c7610de3b0},
|
||||
{.sector = 13, .key_a = 0xa82607b01c0d, .key_b = 0x2910989b6880},
|
||||
{.sector = 14, .key_a = 0x0e8f64340ba4, .key_b = 0x4acec1205d75},
|
||||
{.sector = 15, .key_a = 0x2aa05ed1856f, .key_b = 0xeaac88e5dc99},
|
||||
};
|
||||
|
||||
bool troyka_parser_verify(NfcWorker* nfc_worker, FuriHalNfcTxRxContext* tx_rx) {
|
||||
furi_assert(nfc_worker);
|
||||
UNUSED(nfc_worker);
|
||||
|
||||
MfClassicAuthContext auth_ctx = {
|
||||
.key_a = MF_CLASSIC_NO_KEY,
|
||||
.key_b = MF_CLASSIC_NO_KEY,
|
||||
.sector = 8,
|
||||
};
|
||||
return mf_classic_auth_attempt(tx_rx, &auth_ctx, 0xa73f5dc1d333);
|
||||
}
|
||||
|
||||
bool troyka_parser_read(NfcWorker* nfc_worker, FuriHalNfcTxRxContext* tx_rx) {
|
||||
furi_assert(nfc_worker);
|
||||
|
||||
MfClassicReader reader = {};
|
||||
FuriHalNfcDevData* nfc_data = &nfc_worker->dev_data->nfc_data;
|
||||
mf_classic_get_type(nfc_data->atqa[0], nfc_data->atqa[1], nfc_data->sak, &reader);
|
||||
for(size_t i = 0; i < COUNT_OF(troyka_keys); i++) {
|
||||
mf_classic_reader_add_sector(
|
||||
&reader, troyka_keys[i].sector, troyka_keys[i].key_a, troyka_keys[i].key_b);
|
||||
}
|
||||
|
||||
return mf_classic_read_card(tx_rx, &reader, &nfc_worker->dev_data->mf_classic_data) == 16;
|
||||
}
|
||||
|
||||
bool troyka_parser_parse(NfcWorker* nfc_worker) {
|
||||
MfClassicData* data = &nfc_worker->dev_data->mf_classic_data;
|
||||
uint8_t* temp_ptr = &data->block[8 * 4 + 1].value[5];
|
||||
uint16_t balance = ((temp_ptr[0] << 8) | temp_ptr[1]) / 25;
|
||||
temp_ptr = &data->block[8 * 4].value[3];
|
||||
uint32_t number = 0;
|
||||
for(size_t i = 0; i < 4; i++) {
|
||||
number <<= 8;
|
||||
number |= temp_ptr[i];
|
||||
}
|
||||
number >>= 4;
|
||||
|
||||
string_printf(
|
||||
nfc_worker->dev_data->parsed_data,
|
||||
"Troyka Transport card\nNumber: %ld\nBalance: %d rub",
|
||||
number,
|
||||
balance);
|
||||
|
||||
return true;
|
||||
}
|
||||
9
lib/nfc/parsers/troyka_parser.h
Normal file
9
lib/nfc/parsers/troyka_parser.h
Normal file
@@ -0,0 +1,9 @@
|
||||
#pragma once
|
||||
|
||||
#include "nfc_supported_card.h"
|
||||
|
||||
bool troyka_parser_verify(NfcWorker* nfc_worker, FuriHalNfcTxRxContext* tx_rx);
|
||||
|
||||
bool troyka_parser_read(NfcWorker* nfc_worker, FuriHalNfcTxRxContext* tx_rx);
|
||||
|
||||
bool troyka_parser_parse(NfcWorker* nfc_worker);
|
||||
75
lib/nfc/protocols/crypto1.c
Normal file
75
lib/nfc/protocols/crypto1.c
Normal file
@@ -0,0 +1,75 @@
|
||||
#include "crypto1.h"
|
||||
#include "nfc_util.h"
|
||||
#include <furi.h>
|
||||
|
||||
// Algorithm from https://github.com/RfidResearchGroup/proxmark3.git
|
||||
|
||||
#define SWAPENDIAN(x) (x = (x >> 8 & 0xff00ff) | (x & 0xff00ff) << 8, x = x >> 16 | x << 16)
|
||||
#define LF_POLY_ODD (0x29CE5C)
|
||||
#define LF_POLY_EVEN (0x870804)
|
||||
|
||||
#define BEBIT(x, n) FURI_BIT(x, (n) ^ 24)
|
||||
|
||||
void crypto1_reset(Crypto1* crypto1) {
|
||||
furi_assert(crypto1);
|
||||
crypto1->even = 0;
|
||||
crypto1->odd = 0;
|
||||
}
|
||||
|
||||
void crypto1_init(Crypto1* crypto1, uint64_t key) {
|
||||
furi_assert(crypto1);
|
||||
crypto1->even = 0;
|
||||
crypto1->odd = 0;
|
||||
for(int8_t i = 47; i > 0; i -= 2) {
|
||||
crypto1->odd = crypto1->odd << 1 | FURI_BIT(key, (i - 1) ^ 7);
|
||||
crypto1->even = crypto1->even << 1 | FURI_BIT(key, i ^ 7);
|
||||
}
|
||||
}
|
||||
|
||||
uint32_t crypto1_filter(uint32_t in) {
|
||||
uint32_t out = 0;
|
||||
out = 0xf22c0 >> (in & 0xf) & 16;
|
||||
out |= 0x6c9c0 >> (in >> 4 & 0xf) & 8;
|
||||
out |= 0x3c8b0 >> (in >> 8 & 0xf) & 4;
|
||||
out |= 0x1e458 >> (in >> 12 & 0xf) & 2;
|
||||
out |= 0x0d938 >> (in >> 16 & 0xf) & 1;
|
||||
return FURI_BIT(0xEC57E80A, out);
|
||||
}
|
||||
|
||||
uint8_t crypto1_bit(Crypto1* crypto1, uint8_t in, int is_encrypted) {
|
||||
furi_assert(crypto1);
|
||||
uint8_t out = crypto1_filter(crypto1->odd);
|
||||
uint32_t feed = out & (!!is_encrypted);
|
||||
feed ^= !!in;
|
||||
feed ^= LF_POLY_ODD & crypto1->odd;
|
||||
feed ^= LF_POLY_EVEN & crypto1->even;
|
||||
crypto1->even = crypto1->even << 1 | (nfc_util_even_parity32(feed));
|
||||
|
||||
FURI_SWAP(crypto1->odd, crypto1->even);
|
||||
return out;
|
||||
}
|
||||
|
||||
uint8_t crypto1_byte(Crypto1* crypto1, uint8_t in, int is_encrypted) {
|
||||
furi_assert(crypto1);
|
||||
uint8_t out = 0;
|
||||
for(uint8_t i = 0; i < 8; i++) {
|
||||
out |= crypto1_bit(crypto1, FURI_BIT(in, i), is_encrypted) << i;
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
uint32_t crypto1_word(Crypto1* crypto1, uint32_t in, int is_encrypted) {
|
||||
furi_assert(crypto1);
|
||||
uint32_t out = 0;
|
||||
for(uint8_t i = 0; i < 32; i++) {
|
||||
out |= crypto1_bit(crypto1, BEBIT(in, i), is_encrypted) << (24 ^ i);
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
uint32_t prng_successor(uint32_t x, uint32_t n) {
|
||||
SWAPENDIAN(x);
|
||||
while(n--) x = x >> 1 | (x >> 16 ^ x >> 18 ^ x >> 19 ^ x >> 21) << 31;
|
||||
|
||||
return SWAPENDIAN(x);
|
||||
}
|
||||
23
lib/nfc/protocols/crypto1.h
Normal file
23
lib/nfc/protocols/crypto1.h
Normal file
@@ -0,0 +1,23 @@
|
||||
#pragma once
|
||||
|
||||
#include <stdint.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
typedef struct {
|
||||
uint32_t odd;
|
||||
uint32_t even;
|
||||
} Crypto1;
|
||||
|
||||
void crypto1_reset(Crypto1* crypto1);
|
||||
|
||||
void crypto1_init(Crypto1* crypto1, uint64_t key);
|
||||
|
||||
uint8_t crypto1_bit(Crypto1* crypto1, uint8_t in, int is_encrypted);
|
||||
|
||||
uint8_t crypto1_byte(Crypto1* crypto1, uint8_t in, int is_encrypted);
|
||||
|
||||
uint32_t crypto1_word(Crypto1* crypto1, uint32_t in, int is_encrypted);
|
||||
|
||||
uint32_t crypto1_filter(uint32_t in);
|
||||
|
||||
uint32_t prng_successor(uint32_t x, uint32_t n);
|
||||
429
lib/nfc/protocols/emv.c
Normal file
429
lib/nfc/protocols/emv.c
Normal file
@@ -0,0 +1,429 @@
|
||||
#include "emv.h"
|
||||
|
||||
#include <core/common_defines.h>
|
||||
|
||||
#define TAG "Emv"
|
||||
|
||||
const PDOLValue pdol_term_info = {0x9F59, {0xC8, 0x80, 0x00}}; // Terminal transaction information
|
||||
const PDOLValue pdol_term_type = {0x9F5A, {0x00}}; // Terminal transaction type
|
||||
const PDOLValue pdol_merchant_type = {0x9F58, {0x01}}; // Merchant type indicator
|
||||
const PDOLValue pdol_term_trans_qualifies = {
|
||||
0x9F66,
|
||||
{0x79, 0x00, 0x40, 0x80}}; // Terminal transaction qualifiers
|
||||
const PDOLValue pdol_addtnl_term_qualifies = {
|
||||
0x9F40,
|
||||
{0x79, 0x00, 0x40, 0x80}}; // Terminal transaction qualifiers
|
||||
const PDOLValue pdol_amount_authorise = {
|
||||
0x9F02,
|
||||
{0x00, 0x00, 0x00, 0x10, 0x00, 0x00}}; // Amount, authorised
|
||||
const PDOLValue pdol_amount = {0x9F03, {0x00, 0x00, 0x00, 0x00, 0x00, 0x00}}; // Amount
|
||||
const PDOLValue pdol_country_code = {0x9F1A, {0x01, 0x24}}; // Terminal country code
|
||||
const PDOLValue pdol_currency_code = {0x5F2A, {0x01, 0x24}}; // Transaction currency code
|
||||
const PDOLValue pdol_term_verification = {
|
||||
0x95,
|
||||
{0x00, 0x00, 0x00, 0x00, 0x00}}; // Terminal verification results
|
||||
const PDOLValue pdol_transaction_date = {0x9A, {0x19, 0x01, 0x01}}; // Transaction date
|
||||
const PDOLValue pdol_transaction_type = {0x9C, {0x00}}; // Transaction type
|
||||
const PDOLValue pdol_transaction_cert = {0x98, {0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0}}; // Transaction cert
|
||||
const PDOLValue pdol_unpredict_number = {0x9F37, {0x82, 0x3D, 0xDE, 0x7A}}; // Unpredictable number
|
||||
|
||||
const PDOLValue* const pdol_values[] = {
|
||||
&pdol_term_info,
|
||||
&pdol_term_type,
|
||||
&pdol_merchant_type,
|
||||
&pdol_term_trans_qualifies,
|
||||
&pdol_addtnl_term_qualifies,
|
||||
&pdol_amount_authorise,
|
||||
&pdol_amount,
|
||||
&pdol_country_code,
|
||||
&pdol_currency_code,
|
||||
&pdol_term_verification,
|
||||
&pdol_transaction_date,
|
||||
&pdol_transaction_type,
|
||||
&pdol_transaction_cert,
|
||||
&pdol_unpredict_number,
|
||||
};
|
||||
|
||||
static const uint8_t select_ppse_ans[] = {0x6F, 0x29, 0x84, 0x0E, 0x32, 0x50, 0x41, 0x59, 0x2E,
|
||||
0x53, 0x59, 0x53, 0x2E, 0x44, 0x44, 0x46, 0x30, 0x31,
|
||||
0xA5, 0x17, 0xBF, 0x0C, 0x14, 0x61, 0x12, 0x4F, 0x07,
|
||||
0xA0, 0x00, 0x00, 0x00, 0x03, 0x10, 0x10, 0x50, 0x04,
|
||||
0x56, 0x49, 0x53, 0x41, 0x87, 0x01, 0x01, 0x90, 0x00};
|
||||
static const uint8_t select_app_ans[] = {0x6F, 0x20, 0x84, 0x07, 0xA0, 0x00, 0x00, 0x00, 0x03,
|
||||
0x10, 0x10, 0xA5, 0x15, 0x50, 0x04, 0x56, 0x49, 0x53,
|
||||
0x41, 0x9F, 0x38, 0x0C, 0x9F, 0x66, 0x04, 0x9F, 0x02,
|
||||
0x06, 0x9F, 0x37, 0x04, 0x5F, 0x2A, 0x02, 0x90, 0x00};
|
||||
static const uint8_t pdol_ans[] = {0x77, 0x40, 0x82, 0x02, 0x20, 0x00, 0x57, 0x13, 0x55, 0x70,
|
||||
0x73, 0x83, 0x85, 0x87, 0x73, 0x31, 0xD1, 0x80, 0x22, 0x01,
|
||||
0x38, 0x84, 0x77, 0x94, 0x00, 0x00, 0x1F, 0x5F, 0x34, 0x01,
|
||||
0x00, 0x9F, 0x10, 0x07, 0x06, 0x01, 0x11, 0x03, 0x80, 0x00,
|
||||
0x00, 0x9F, 0x26, 0x08, 0x7A, 0x65, 0x7F, 0xD3, 0x52, 0x96,
|
||||
0xC9, 0x85, 0x9F, 0x27, 0x01, 0x00, 0x9F, 0x36, 0x02, 0x06,
|
||||
0x0C, 0x9F, 0x6C, 0x02, 0x10, 0x00, 0x90, 0x00};
|
||||
|
||||
static void emv_trace(FuriHalNfcTxRxContext* tx_rx, const char* message) {
|
||||
if(furi_log_get_level() == FuriLogLevelTrace) {
|
||||
FURI_LOG_T(TAG, "%s", message);
|
||||
printf("TX: ");
|
||||
for(size_t i = 0; i < tx_rx->tx_bits / 8; i++) {
|
||||
printf("%02X ", tx_rx->tx_data[i]);
|
||||
}
|
||||
printf("\r\nRX: ");
|
||||
for(size_t i = 0; i < tx_rx->rx_bits / 8; i++) {
|
||||
printf("%02X ", tx_rx->rx_data[i]);
|
||||
}
|
||||
printf("\r\n");
|
||||
}
|
||||
}
|
||||
|
||||
static bool emv_decode_response(uint8_t* buff, uint16_t len, EmvApplication* app) {
|
||||
uint16_t i = 0;
|
||||
uint16_t tag = 0, first_byte = 0;
|
||||
uint16_t tlen = 0;
|
||||
bool success = false;
|
||||
|
||||
while(i < len) {
|
||||
first_byte = buff[i];
|
||||
if((first_byte & 31) == 31) { // 2-byte tag
|
||||
tag = buff[i] << 8 | buff[i + 1];
|
||||
i++;
|
||||
FURI_LOG_T(TAG, " 2-byte TLV EMV tag: %x", tag);
|
||||
} else {
|
||||
tag = buff[i];
|
||||
FURI_LOG_T(TAG, " 1-byte TLV EMV tag: %x", tag);
|
||||
}
|
||||
i++;
|
||||
tlen = buff[i];
|
||||
if((tlen & 128) == 128) { // long length value
|
||||
i++;
|
||||
tlen = buff[i];
|
||||
FURI_LOG_T(TAG, " 2-byte TLV length: %d", tlen);
|
||||
} else {
|
||||
FURI_LOG_T(TAG, " 1-byte TLV length: %d", tlen);
|
||||
}
|
||||
i++;
|
||||
if((first_byte & 32) == 32) { // "Constructed" -- contains more TLV data to parse
|
||||
FURI_LOG_T(TAG, "Constructed TLV %x", tag);
|
||||
if(!emv_decode_response(&buff[i], tlen, app)) {
|
||||
FURI_LOG_T(TAG, "Failed to decode response for %x", tag);
|
||||
// return false;
|
||||
} else {
|
||||
success = true;
|
||||
}
|
||||
} else {
|
||||
switch(tag) {
|
||||
case EMV_TAG_AID:
|
||||
app->aid_len = tlen;
|
||||
memcpy(app->aid, &buff[i], tlen);
|
||||
success = true;
|
||||
FURI_LOG_T(TAG, "found EMV_TAG_AID %x", tag);
|
||||
break;
|
||||
case EMV_TAG_PRIORITY:
|
||||
memcpy(&app->priority, &buff[i], tlen);
|
||||
success = true;
|
||||
break;
|
||||
case EMV_TAG_CARD_NAME:
|
||||
memcpy(app->name, &buff[i], tlen);
|
||||
app->name[tlen] = '\0';
|
||||
app->name_found = true;
|
||||
success = true;
|
||||
FURI_LOG_T(TAG, "found EMV_TAG_CARD_NAME %x : %s", tag, app->name);
|
||||
break;
|
||||
case EMV_TAG_PDOL:
|
||||
memcpy(app->pdol.data, &buff[i], tlen);
|
||||
app->pdol.size = tlen;
|
||||
success = true;
|
||||
FURI_LOG_T(TAG, "found EMV_TAG_PDOL %x (len=%d)", tag, tlen);
|
||||
break;
|
||||
case EMV_TAG_AFL:
|
||||
memcpy(app->afl.data, &buff[i], tlen);
|
||||
app->afl.size = tlen;
|
||||
success = true;
|
||||
FURI_LOG_T(TAG, "found EMV_TAG_AFL %x (len=%d)", tag, tlen);
|
||||
break;
|
||||
case EMV_TAG_CARD_NUM: // Track 2 Equivalent Data. 0xD0 delimits PAN from expiry (YYMM)
|
||||
for(int x = 1; x < tlen; x++) {
|
||||
if(buff[i + x + 1] > 0xD0) {
|
||||
memcpy(app->card_number, &buff[i], x + 1);
|
||||
app->card_number_len = x + 1;
|
||||
break;
|
||||
}
|
||||
}
|
||||
success = true;
|
||||
FURI_LOG_T(
|
||||
TAG,
|
||||
"found EMV_TAG_CARD_NUM %x (len=%d)",
|
||||
EMV_TAG_CARD_NUM,
|
||||
app->card_number_len);
|
||||
break;
|
||||
case EMV_TAG_PAN:
|
||||
memcpy(app->card_number, &buff[i], tlen);
|
||||
app->card_number_len = tlen;
|
||||
success = true;
|
||||
break;
|
||||
case EMV_TAG_EXP_DATE:
|
||||
app->exp_year = buff[i];
|
||||
app->exp_month = buff[i + 1];
|
||||
success = true;
|
||||
break;
|
||||
case EMV_TAG_CURRENCY_CODE:
|
||||
app->currency_code = (buff[i] << 8 | buff[i + 1]);
|
||||
success = true;
|
||||
break;
|
||||
case EMV_TAG_COUNTRY_CODE:
|
||||
app->country_code = (buff[i] << 8 | buff[i + 1]);
|
||||
success = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
i += tlen;
|
||||
}
|
||||
return success;
|
||||
}
|
||||
|
||||
bool emv_select_ppse(FuriHalNfcTxRxContext* tx_rx, EmvApplication* app) {
|
||||
bool app_aid_found = false;
|
||||
const uint8_t emv_select_ppse_cmd[] = {
|
||||
0x00, 0xA4, // SELECT ppse
|
||||
0x04, 0x00, // P1:By name, P2: empty
|
||||
0x0e, // Lc: Data length
|
||||
0x32, 0x50, 0x41, 0x59, 0x2e, 0x53, 0x59, // Data string:
|
||||
0x53, 0x2e, 0x44, 0x44, 0x46, 0x30, 0x31, // 2PAY.SYS.DDF01 (PPSE)
|
||||
0x00 // Le
|
||||
};
|
||||
|
||||
memcpy(tx_rx->tx_data, emv_select_ppse_cmd, sizeof(emv_select_ppse_cmd));
|
||||
tx_rx->tx_bits = sizeof(emv_select_ppse_cmd) * 8;
|
||||
tx_rx->tx_rx_type = FuriHalNfcTxRxTypeDefault;
|
||||
|
||||
FURI_LOG_D(TAG, "Send select PPSE");
|
||||
if(furi_hal_nfc_tx_rx(tx_rx, 300)) {
|
||||
emv_trace(tx_rx, "Select PPSE answer:");
|
||||
if(emv_decode_response(tx_rx->rx_data, tx_rx->rx_bits / 8, app)) {
|
||||
app_aid_found = true;
|
||||
} else {
|
||||
FURI_LOG_E(TAG, "Failed to parse application");
|
||||
}
|
||||
} else {
|
||||
FURI_LOG_E(TAG, "Failed select PPSE");
|
||||
}
|
||||
|
||||
return app_aid_found;
|
||||
}
|
||||
|
||||
bool emv_select_app(FuriHalNfcTxRxContext* tx_rx, EmvApplication* app) {
|
||||
bool select_app_success = false;
|
||||
const uint8_t emv_select_header[] = {
|
||||
0x00,
|
||||
0xA4, // SELECT application
|
||||
0x04,
|
||||
0x00 // P1:By name, P2:First or only occurence
|
||||
};
|
||||
uint16_t size = sizeof(emv_select_header);
|
||||
|
||||
// Copy header
|
||||
memcpy(tx_rx->tx_data, emv_select_header, size);
|
||||
// Copy AID
|
||||
tx_rx->tx_data[size++] = app->aid_len;
|
||||
memcpy(&tx_rx->tx_data[size], app->aid, app->aid_len);
|
||||
size += app->aid_len;
|
||||
tx_rx->tx_data[size++] = 0x00;
|
||||
tx_rx->tx_bits = size * 8;
|
||||
tx_rx->tx_rx_type = FuriHalNfcTxRxTypeDefault;
|
||||
|
||||
FURI_LOG_D(TAG, "Start application");
|
||||
if(furi_hal_nfc_tx_rx(tx_rx, 300)) {
|
||||
emv_trace(tx_rx, "Start application answer:");
|
||||
if(emv_decode_response(tx_rx->rx_data, tx_rx->rx_bits / 8, app)) {
|
||||
select_app_success = true;
|
||||
} else {
|
||||
FURI_LOG_E(TAG, "Failed to read PAN or PDOL");
|
||||
}
|
||||
} else {
|
||||
FURI_LOG_E(TAG, "Failed to start application");
|
||||
}
|
||||
|
||||
return select_app_success;
|
||||
}
|
||||
|
||||
static uint16_t emv_prepare_pdol(APDU* dest, APDU* src) {
|
||||
bool tag_found;
|
||||
for(uint16_t i = 0; i < src->size; i++) {
|
||||
tag_found = false;
|
||||
for(uint8_t j = 0; j < sizeof(pdol_values) / sizeof(PDOLValue*); j++) {
|
||||
if(src->data[i] == pdol_values[j]->tag) {
|
||||
// Found tag with 1 byte length
|
||||
uint8_t len = src->data[++i];
|
||||
memcpy(dest->data + dest->size, pdol_values[j]->data, len);
|
||||
dest->size += len;
|
||||
tag_found = true;
|
||||
break;
|
||||
} else if(((src->data[i] << 8) | src->data[i + 1]) == pdol_values[j]->tag) {
|
||||
// Found tag with 2 byte length
|
||||
i += 2;
|
||||
uint8_t len = src->data[i];
|
||||
memcpy(dest->data + dest->size, pdol_values[j]->data, len);
|
||||
dest->size += len;
|
||||
tag_found = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if(!tag_found) {
|
||||
// Unknown tag, fill zeros
|
||||
i += 2;
|
||||
uint8_t len = src->data[i];
|
||||
memset(dest->data + dest->size, 0, len);
|
||||
dest->size += len;
|
||||
}
|
||||
}
|
||||
return dest->size;
|
||||
}
|
||||
|
||||
static bool emv_get_processing_options(FuriHalNfcTxRxContext* tx_rx, EmvApplication* app) {
|
||||
bool card_num_read = false;
|
||||
const uint8_t emv_gpo_header[] = {0x80, 0xA8, 0x00, 0x00};
|
||||
uint16_t size = sizeof(emv_gpo_header);
|
||||
|
||||
// Copy header
|
||||
memcpy(tx_rx->tx_data, emv_gpo_header, size);
|
||||
APDU pdol_data = {0, {0}};
|
||||
// Prepare and copy pdol parameters
|
||||
emv_prepare_pdol(&pdol_data, &app->pdol);
|
||||
tx_rx->tx_data[size++] = 0x02 + pdol_data.size;
|
||||
tx_rx->tx_data[size++] = 0x83;
|
||||
tx_rx->tx_data[size++] = pdol_data.size;
|
||||
memcpy(tx_rx->tx_data + size, pdol_data.data, pdol_data.size);
|
||||
size += pdol_data.size;
|
||||
tx_rx->tx_data[size++] = 0;
|
||||
tx_rx->tx_bits = size * 8;
|
||||
tx_rx->tx_rx_type = FuriHalNfcTxRxTypeDefault;
|
||||
|
||||
FURI_LOG_D(TAG, "Get proccessing options");
|
||||
if(furi_hal_nfc_tx_rx(tx_rx, 300)) {
|
||||
emv_trace(tx_rx, "Get processing options answer:");
|
||||
if(emv_decode_response(tx_rx->rx_data, tx_rx->rx_bits / 8, app)) {
|
||||
if(app->card_number_len > 0) {
|
||||
card_num_read = true;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
FURI_LOG_E(TAG, "Failed to get processing options");
|
||||
}
|
||||
|
||||
return card_num_read;
|
||||
}
|
||||
|
||||
static bool emv_read_sfi_record(
|
||||
FuriHalNfcTxRxContext* tx_rx,
|
||||
EmvApplication* app,
|
||||
uint8_t sfi,
|
||||
uint8_t record_num) {
|
||||
bool card_num_read = false;
|
||||
uint8_t sfi_param = (sfi << 3) | (1 << 2);
|
||||
uint8_t emv_sfi_header[] = {
|
||||
0x00,
|
||||
0xB2, // READ RECORD
|
||||
record_num, // P1:record_number
|
||||
sfi_param, // P2:SFI
|
||||
0x00 // Le
|
||||
};
|
||||
|
||||
memcpy(tx_rx->tx_data, emv_sfi_header, sizeof(emv_sfi_header));
|
||||
tx_rx->tx_bits = sizeof(emv_sfi_header) * 8;
|
||||
tx_rx->tx_rx_type = FuriHalNfcTxRxTypeDefault;
|
||||
|
||||
if(furi_hal_nfc_tx_rx(tx_rx, 300)) {
|
||||
emv_trace(tx_rx, "SFI record:");
|
||||
if(emv_decode_response(tx_rx->rx_data, tx_rx->rx_bits / 8, app)) {
|
||||
card_num_read = true;
|
||||
}
|
||||
} else {
|
||||
FURI_LOG_E(TAG, "Failed to read SFI record %d", record_num);
|
||||
}
|
||||
|
||||
return card_num_read;
|
||||
}
|
||||
|
||||
static bool emv_read_files(FuriHalNfcTxRxContext* tx_rx, EmvApplication* app) {
|
||||
bool card_num_read = false;
|
||||
|
||||
if(app->afl.size == 0) {
|
||||
return false;
|
||||
}
|
||||
|
||||
FURI_LOG_D(TAG, "Search PAN in SFI");
|
||||
// Iterate through all files
|
||||
for(size_t i = 0; i < app->afl.size; i += 4) {
|
||||
uint8_t sfi = app->afl.data[i] >> 3;
|
||||
uint8_t record_start = app->afl.data[i + 1];
|
||||
uint8_t record_end = app->afl.data[i + 2];
|
||||
// Iterate through all records in file
|
||||
for(uint8_t record = record_start; record <= record_end; ++record) {
|
||||
card_num_read |= emv_read_sfi_record(tx_rx, app, sfi, record);
|
||||
}
|
||||
}
|
||||
|
||||
return card_num_read;
|
||||
}
|
||||
|
||||
bool emv_search_application(FuriHalNfcTxRxContext* tx_rx, EmvApplication* emv_app) {
|
||||
furi_assert(tx_rx);
|
||||
furi_assert(emv_app);
|
||||
memset(emv_app, 0, sizeof(EmvApplication));
|
||||
|
||||
return emv_select_ppse(tx_rx, emv_app);
|
||||
}
|
||||
|
||||
bool emv_read_bank_card(FuriHalNfcTxRxContext* tx_rx, EmvApplication* emv_app) {
|
||||
furi_assert(tx_rx);
|
||||
furi_assert(emv_app);
|
||||
bool card_num_read = false;
|
||||
memset(emv_app, 0, sizeof(EmvApplication));
|
||||
|
||||
do {
|
||||
if(!emv_select_ppse(tx_rx, emv_app)) break;
|
||||
if(!emv_select_app(tx_rx, emv_app)) break;
|
||||
if(emv_get_processing_options(tx_rx, emv_app)) {
|
||||
card_num_read = true;
|
||||
} else {
|
||||
card_num_read = emv_read_files(tx_rx, emv_app);
|
||||
}
|
||||
} while(false);
|
||||
|
||||
return card_num_read;
|
||||
}
|
||||
|
||||
bool emv_card_emulation(FuriHalNfcTxRxContext* tx_rx) {
|
||||
furi_assert(tx_rx);
|
||||
bool emulation_complete = false;
|
||||
tx_rx->tx_bits = 0;
|
||||
tx_rx->tx_rx_type = FuriHalNfcTxRxTypeDefault;
|
||||
|
||||
do {
|
||||
FURI_LOG_D(TAG, "Read select PPSE command");
|
||||
if(!furi_hal_nfc_tx_rx(tx_rx, 300)) break;
|
||||
|
||||
memcpy(tx_rx->tx_data, select_ppse_ans, sizeof(select_ppse_ans));
|
||||
tx_rx->tx_bits = sizeof(select_ppse_ans) * 8;
|
||||
tx_rx->tx_rx_type = FuriHalNfcTxRxTypeDefault;
|
||||
FURI_LOG_D(TAG, "Send select PPSE answer and read select App command");
|
||||
if(!furi_hal_nfc_tx_rx(tx_rx, 300)) break;
|
||||
|
||||
memcpy(tx_rx->tx_data, select_app_ans, sizeof(select_app_ans));
|
||||
tx_rx->tx_bits = sizeof(select_app_ans) * 8;
|
||||
tx_rx->tx_rx_type = FuriHalNfcTxRxTypeDefault;
|
||||
FURI_LOG_D(TAG, "Send select App answer and read get PDOL command");
|
||||
if(!furi_hal_nfc_tx_rx(tx_rx, 300)) break;
|
||||
|
||||
memcpy(tx_rx->tx_data, pdol_ans, sizeof(pdol_ans));
|
||||
tx_rx->tx_bits = sizeof(pdol_ans) * 8;
|
||||
tx_rx->tx_rx_type = FuriHalNfcTxRxTypeDefault;
|
||||
FURI_LOG_D(TAG, "Send get PDOL answer");
|
||||
if(!furi_hal_nfc_tx_rx(tx_rx, 300)) break;
|
||||
|
||||
emulation_complete = true;
|
||||
} while(false);
|
||||
|
||||
return emulation_complete;
|
||||
}
|
||||
87
lib/nfc/protocols/emv.h
Executable file
87
lib/nfc/protocols/emv.h
Executable file
@@ -0,0 +1,87 @@
|
||||
#pragma once
|
||||
|
||||
#include <furi_hal_nfc.h>
|
||||
|
||||
#define MAX_APDU_LEN 255
|
||||
|
||||
#define EMV_TAG_APP_TEMPLATE 0x61
|
||||
#define EMV_TAG_AID 0x4F
|
||||
#define EMV_TAG_PRIORITY 0x87
|
||||
#define EMV_TAG_PDOL 0x9F38
|
||||
#define EMV_TAG_CARD_NAME 0x50
|
||||
#define EMV_TAG_FCI 0xBF0C
|
||||
#define EMV_TAG_LOG_CTRL 0x9F4D
|
||||
#define EMV_TAG_CARD_NUM 0x57
|
||||
#define EMV_TAG_PAN 0x5A
|
||||
#define EMV_TAG_AFL 0x94
|
||||
#define EMV_TAG_EXP_DATE 0x5F24
|
||||
#define EMV_TAG_COUNTRY_CODE 0x5F28
|
||||
#define EMV_TAG_CURRENCY_CODE 0x9F42
|
||||
#define EMV_TAG_CARDHOLDER_NAME 0x5F20
|
||||
|
||||
typedef struct {
|
||||
char name[32];
|
||||
uint8_t aid[16];
|
||||
uint16_t aid_len;
|
||||
uint8_t number[10];
|
||||
uint8_t number_len;
|
||||
uint8_t exp_mon;
|
||||
uint8_t exp_year;
|
||||
uint16_t country_code;
|
||||
uint16_t currency_code;
|
||||
} EmvData;
|
||||
|
||||
typedef struct {
|
||||
uint16_t tag;
|
||||
uint8_t data[];
|
||||
} PDOLValue;
|
||||
|
||||
typedef struct {
|
||||
uint8_t size;
|
||||
uint8_t data[MAX_APDU_LEN];
|
||||
} APDU;
|
||||
|
||||
typedef struct {
|
||||
uint8_t priority;
|
||||
uint8_t aid[16];
|
||||
uint8_t aid_len;
|
||||
char name[32];
|
||||
bool name_found;
|
||||
uint8_t card_number[10];
|
||||
uint8_t card_number_len;
|
||||
uint8_t exp_month;
|
||||
uint8_t exp_year;
|
||||
uint16_t country_code;
|
||||
uint16_t currency_code;
|
||||
APDU pdol;
|
||||
APDU afl;
|
||||
} EmvApplication;
|
||||
|
||||
/** Read bank card data
|
||||
* @note Search EMV Application, start it, try to read AID, PAN, card name,
|
||||
* expiration date, currency and country codes
|
||||
*
|
||||
* @param tx_rx FuriHalNfcTxRxContext instance
|
||||
* @param emv_app EmvApplication instance
|
||||
*
|
||||
* @return true on success
|
||||
*/
|
||||
bool emv_read_bank_card(FuriHalNfcTxRxContext* tx_rx, EmvApplication* emv_app);
|
||||
|
||||
/** Search for EMV Application
|
||||
*
|
||||
* @param tx_rx FuriHalNfcTxRxContext instance
|
||||
* @param emv_app EmvApplication instance
|
||||
*
|
||||
* @return true on success
|
||||
*/
|
||||
bool emv_search_application(FuriHalNfcTxRxContext* tx_rx, EmvApplication* emv_app);
|
||||
|
||||
/** Emulate bank card
|
||||
* @note Answer to application selection and PDOL
|
||||
*
|
||||
* @param tx_rx FuriHalNfcTxRxContext instance
|
||||
*
|
||||
* @return true on success
|
||||
*/
|
||||
bool emv_card_emulation(FuriHalNfcTxRxContext* tx_rx);
|
||||
989
lib/nfc/protocols/mifare_classic.c
Normal file
989
lib/nfc/protocols/mifare_classic.c
Normal file
@@ -0,0 +1,989 @@
|
||||
#include "mifare_classic.h"
|
||||
#include "nfca.h"
|
||||
#include "nfc_util.h"
|
||||
#include <furi_hal_rtc.h>
|
||||
|
||||
// Algorithm from https://github.com/RfidResearchGroup/proxmark3.git
|
||||
|
||||
#define TAG "MfClassic"
|
||||
|
||||
#define MF_CLASSIC_AUTH_KEY_A_CMD (0x60U)
|
||||
#define MF_CLASSIC_AUTH_KEY_B_CMD (0x61U)
|
||||
#define MF_CLASSIC_READ_SECT_CMD (0x30)
|
||||
|
||||
typedef enum {
|
||||
MfClassicActionDataRead,
|
||||
MfClassicActionDataWrite,
|
||||
MfClassicActionDataInc,
|
||||
MfClassicActionDataDec,
|
||||
|
||||
MfClassicActionKeyARead,
|
||||
MfClassicActionKeyAWrite,
|
||||
MfClassicActionKeyBRead,
|
||||
MfClassicActionKeyBWrite,
|
||||
MfClassicActionACRead,
|
||||
MfClassicActionACWrite,
|
||||
} MfClassicAction;
|
||||
|
||||
const char* mf_classic_get_type_str(MfClassicType type) {
|
||||
if(type == MfClassicType1k) {
|
||||
return "MIFARE Classic 1K";
|
||||
} else if(type == MfClassicType4k) {
|
||||
return "MIFARE Classic 4K";
|
||||
} else {
|
||||
return "Unknown";
|
||||
}
|
||||
}
|
||||
|
||||
static uint8_t mf_classic_get_first_block_num_of_sector(uint8_t sector) {
|
||||
furi_assert(sector < 40);
|
||||
if(sector < 32) {
|
||||
return sector * 4;
|
||||
} else {
|
||||
return 32 * 4 + (sector - 32) * 16;
|
||||
}
|
||||
}
|
||||
|
||||
uint8_t mf_classic_get_sector_trailer_block_num_by_sector(uint8_t sector) {
|
||||
furi_assert(sector < 40);
|
||||
if(sector < 32) {
|
||||
return sector * 4 + 3;
|
||||
} else {
|
||||
return 32 * 4 + (sector - 32) * 16 + 15;
|
||||
}
|
||||
}
|
||||
|
||||
uint8_t mf_classic_get_sector_by_block(uint8_t block) {
|
||||
if(block < 128) {
|
||||
return (block | 0x03) / 4;
|
||||
} else {
|
||||
return 32 + ((block | 0xf) - 32 * 4) / 16;
|
||||
}
|
||||
}
|
||||
|
||||
static uint8_t mf_classic_get_blocks_num_in_sector(uint8_t sector) {
|
||||
furi_assert(sector < 40);
|
||||
return sector < 32 ? 4 : 16;
|
||||
}
|
||||
|
||||
uint8_t mf_classic_get_sector_trailer_num_by_block(uint8_t block) {
|
||||
if(block < 128) {
|
||||
return block | 0x03;
|
||||
} else {
|
||||
return block | 0x0f;
|
||||
}
|
||||
}
|
||||
|
||||
bool mf_classic_is_sector_trailer(uint8_t block) {
|
||||
return block == mf_classic_get_sector_trailer_num_by_block(block);
|
||||
}
|
||||
|
||||
MfClassicSectorTrailer*
|
||||
mf_classic_get_sector_trailer_by_sector(MfClassicData* data, uint8_t sector) {
|
||||
furi_assert(data);
|
||||
uint8_t sec_tr_block_num = mf_classic_get_sector_trailer_block_num_by_sector(sector);
|
||||
return (MfClassicSectorTrailer*)data->block[sec_tr_block_num].value;
|
||||
}
|
||||
|
||||
uint8_t mf_classic_get_total_sectors_num(MfClassicType type) {
|
||||
if(type == MfClassicType1k) {
|
||||
return MF_CLASSIC_1K_TOTAL_SECTORS_NUM;
|
||||
} else if(type == MfClassicType4k) {
|
||||
return MF_CLASSIC_4K_TOTAL_SECTORS_NUM;
|
||||
} else {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
static uint16_t mf_classic_get_total_block_num(MfClassicType type) {
|
||||
if(type == MfClassicType1k) {
|
||||
return 64;
|
||||
} else if(type == MfClassicType4k) {
|
||||
return 256;
|
||||
} else {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
bool mf_classic_is_block_read(MfClassicData* data, uint8_t block_num) {
|
||||
furi_assert(data);
|
||||
|
||||
return (FURI_BIT(data->block_read_mask[block_num / 32], block_num % 32) == 1);
|
||||
}
|
||||
|
||||
void mf_classic_set_block_read(MfClassicData* data, uint8_t block_num, MfClassicBlock* block_data) {
|
||||
furi_assert(data);
|
||||
|
||||
if(mf_classic_is_sector_trailer(block_num)) {
|
||||
memcpy(&data->block[block_num].value[6], &block_data->value[6], 4);
|
||||
} else {
|
||||
memcpy(data->block[block_num].value, block_data->value, MF_CLASSIC_BLOCK_SIZE);
|
||||
}
|
||||
FURI_BIT_SET(data->block_read_mask[block_num / 32], block_num % 32);
|
||||
}
|
||||
|
||||
bool mf_classic_is_key_found(MfClassicData* data, uint8_t sector_num, MfClassicKey key_type) {
|
||||
furi_assert(data);
|
||||
|
||||
bool key_found = false;
|
||||
if(key_type == MfClassicKeyA) {
|
||||
key_found = (FURI_BIT(data->key_a_mask, sector_num) == 1);
|
||||
} else if(key_type == MfClassicKeyB) {
|
||||
key_found = (FURI_BIT(data->key_b_mask, sector_num) == 1);
|
||||
}
|
||||
|
||||
return key_found;
|
||||
}
|
||||
|
||||
void mf_classic_set_key_found(
|
||||
MfClassicData* data,
|
||||
uint8_t sector_num,
|
||||
MfClassicKey key_type,
|
||||
uint64_t key) {
|
||||
furi_assert(data);
|
||||
|
||||
uint8_t key_arr[6] = {};
|
||||
MfClassicSectorTrailer* sec_trailer =
|
||||
mf_classic_get_sector_trailer_by_sector(data, sector_num);
|
||||
nfc_util_num2bytes(key, 6, key_arr);
|
||||
if(key_type == MfClassicKeyA) {
|
||||
memcpy(sec_trailer->key_a, key_arr, sizeof(sec_trailer->key_a));
|
||||
FURI_BIT_SET(data->key_a_mask, sector_num);
|
||||
} else if(key_type == MfClassicKeyB) {
|
||||
memcpy(sec_trailer->key_b, key_arr, sizeof(sec_trailer->key_b));
|
||||
FURI_BIT_SET(data->key_b_mask, sector_num);
|
||||
}
|
||||
}
|
||||
|
||||
bool mf_classic_is_sector_read(MfClassicData* data, uint8_t sector_num) {
|
||||
furi_assert(data);
|
||||
|
||||
bool sector_read = false;
|
||||
do {
|
||||
if(!mf_classic_is_key_found(data, sector_num, MfClassicKeyA)) break;
|
||||
if(!mf_classic_is_key_found(data, sector_num, MfClassicKeyB)) break;
|
||||
uint8_t start_block = mf_classic_get_first_block_num_of_sector(sector_num);
|
||||
uint8_t total_blocks = mf_classic_get_blocks_num_in_sector(sector_num);
|
||||
uint8_t block_read = true;
|
||||
for(size_t i = start_block; i < start_block + total_blocks; i++) {
|
||||
block_read = mf_classic_is_block_read(data, i);
|
||||
if(!block_read) break;
|
||||
}
|
||||
sector_read = block_read;
|
||||
} while(false);
|
||||
|
||||
return sector_read;
|
||||
}
|
||||
|
||||
void mf_classic_get_read_sectors_and_keys(
|
||||
MfClassicData* data,
|
||||
uint8_t* sectors_read,
|
||||
uint8_t* keys_found) {
|
||||
furi_assert(data);
|
||||
*sectors_read = 0;
|
||||
*keys_found = 0;
|
||||
uint8_t sectors_total = mf_classic_get_total_sectors_num(data->type);
|
||||
for(size_t i = 0; i < sectors_total; i++) {
|
||||
if(mf_classic_is_key_found(data, i, MfClassicKeyA)) {
|
||||
*keys_found += 1;
|
||||
}
|
||||
if(mf_classic_is_key_found(data, i, MfClassicKeyB)) {
|
||||
*keys_found += 1;
|
||||
}
|
||||
uint8_t first_block = mf_classic_get_first_block_num_of_sector(i);
|
||||
uint8_t total_blocks_in_sec = mf_classic_get_blocks_num_in_sector(i);
|
||||
bool blocks_read = true;
|
||||
for(size_t i = first_block; i < first_block + total_blocks_in_sec; i++) {
|
||||
blocks_read = mf_classic_is_block_read(data, i);
|
||||
if(!blocks_read) break;
|
||||
}
|
||||
if(blocks_read) {
|
||||
*sectors_read += 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static bool mf_classic_is_allowed_access_sector_trailer(
|
||||
MfClassicEmulator* emulator,
|
||||
uint8_t block_num,
|
||||
MfClassicKey key,
|
||||
MfClassicAction action) {
|
||||
uint8_t* sector_trailer = emulator->data.block[block_num].value;
|
||||
uint8_t AC = ((sector_trailer[7] >> 5) & 0x04) | ((sector_trailer[8] >> 2) & 0x02) |
|
||||
((sector_trailer[8] >> 7) & 0x01);
|
||||
switch(action) {
|
||||
case MfClassicActionKeyARead: {
|
||||
return false;
|
||||
}
|
||||
case MfClassicActionKeyAWrite: {
|
||||
return (
|
||||
(key == MfClassicKeyA && (AC == 0x00 || AC == 0x01)) ||
|
||||
(key == MfClassicKeyB && (AC == 0x04 || AC == 0x03)));
|
||||
}
|
||||
case MfClassicActionKeyBRead: {
|
||||
return (key == MfClassicKeyA && (AC == 0x00 || AC == 0x02 || AC == 0x01));
|
||||
}
|
||||
case MfClassicActionKeyBWrite: {
|
||||
return (
|
||||
(key == MfClassicKeyA && (AC == 0x00 || AC == 0x01)) ||
|
||||
(key == MfClassicKeyB && (AC == 0x04 || AC == 0x03)));
|
||||
}
|
||||
case MfClassicActionACRead: {
|
||||
return (
|
||||
(key == MfClassicKeyA) ||
|
||||
(key == MfClassicKeyB && !(AC == 0x00 || AC == 0x02 || AC == 0x01)));
|
||||
}
|
||||
case MfClassicActionACWrite: {
|
||||
return (
|
||||
(key == MfClassicKeyA && (AC == 0x01)) ||
|
||||
(key == MfClassicKeyB && (AC == 0x03 || AC == 0x05)));
|
||||
}
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
static bool mf_classic_is_allowed_access_data_block(
|
||||
MfClassicEmulator* emulator,
|
||||
uint8_t block_num,
|
||||
MfClassicKey key,
|
||||
MfClassicAction action) {
|
||||
uint8_t* sector_trailer =
|
||||
emulator->data.block[mf_classic_get_sector_trailer_num_by_block(block_num)].value;
|
||||
|
||||
uint8_t sector_block;
|
||||
if(block_num <= 128) {
|
||||
sector_block = block_num & 0x03;
|
||||
} else {
|
||||
sector_block = (block_num & 0x0f) / 5;
|
||||
}
|
||||
|
||||
uint8_t AC;
|
||||
switch(sector_block) {
|
||||
case 0x00: {
|
||||
AC = ((sector_trailer[7] >> 2) & 0x04) | ((sector_trailer[8] << 1) & 0x02) |
|
||||
((sector_trailer[8] >> 4) & 0x01);
|
||||
break;
|
||||
}
|
||||
case 0x01: {
|
||||
AC = ((sector_trailer[7] >> 3) & 0x04) | ((sector_trailer[8] >> 0) & 0x02) |
|
||||
((sector_trailer[8] >> 5) & 0x01);
|
||||
break;
|
||||
}
|
||||
case 0x02: {
|
||||
AC = ((sector_trailer[7] >> 4) & 0x04) | ((sector_trailer[8] >> 1) & 0x02) |
|
||||
((sector_trailer[8] >> 6) & 0x01);
|
||||
break;
|
||||
}
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
|
||||
switch(action) {
|
||||
case MfClassicActionDataRead: {
|
||||
return (
|
||||
(key == MfClassicKeyA && !(AC == 0x03 || AC == 0x05 || AC == 0x07)) ||
|
||||
(key == MfClassicKeyB && !(AC == 0x07)));
|
||||
}
|
||||
case MfClassicActionDataWrite: {
|
||||
return (
|
||||
(key == MfClassicKeyA && (AC == 0x00)) ||
|
||||
(key == MfClassicKeyB && (AC == 0x00 || AC == 0x04 || AC == 0x06 || AC == 0x03)));
|
||||
}
|
||||
case MfClassicActionDataInc: {
|
||||
return (
|
||||
(key == MfClassicKeyA && (AC == 0x00)) ||
|
||||
(key == MfClassicKeyB && (AC == 0x00 || AC == 0x06)));
|
||||
}
|
||||
case MfClassicActionDataDec: {
|
||||
return (
|
||||
(key == MfClassicKeyA && (AC == 0x00 || AC == 0x06 || AC == 0x01)) ||
|
||||
(key == MfClassicKeyB && (AC == 0x00 || AC == 0x06 || AC == 0x01)));
|
||||
}
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
static bool mf_classic_is_allowed_access(
|
||||
MfClassicEmulator* emulator,
|
||||
uint8_t block_num,
|
||||
MfClassicKey key,
|
||||
MfClassicAction action) {
|
||||
if(mf_classic_is_sector_trailer(block_num)) {
|
||||
return mf_classic_is_allowed_access_sector_trailer(emulator, block_num, key, action);
|
||||
} else {
|
||||
return mf_classic_is_allowed_access_data_block(emulator, block_num, key, action);
|
||||
}
|
||||
}
|
||||
|
||||
bool mf_classic_check_card_type(uint8_t ATQA0, uint8_t ATQA1, uint8_t SAK) {
|
||||
UNUSED(ATQA1);
|
||||
if((ATQA0 == 0x44 || ATQA0 == 0x04) && (SAK == 0x08 || SAK == 0x88 || SAK == 0x09)) {
|
||||
return true;
|
||||
} else if((ATQA0 == 0x42 || ATQA0 == 0x02) && (SAK == 0x18)) {
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
MfClassicType mf_classic_get_classic_type(int8_t ATQA0, uint8_t ATQA1, uint8_t SAK) {
|
||||
UNUSED(ATQA1);
|
||||
if((ATQA0 == 0x44 || ATQA0 == 0x04) && (SAK == 0x08 || SAK == 0x88 || SAK == 0x09)) {
|
||||
return MfClassicType1k;
|
||||
} else if((ATQA0 == 0x42 || ATQA0 == 0x02) && (SAK == 0x18)) {
|
||||
return MfClassicType4k;
|
||||
}
|
||||
return MfClassicType1k;
|
||||
}
|
||||
|
||||
bool mf_classic_get_type(uint8_t ATQA0, uint8_t ATQA1, uint8_t SAK, MfClassicReader* reader) {
|
||||
UNUSED(ATQA1);
|
||||
furi_assert(reader);
|
||||
memset(reader, 0, sizeof(MfClassicReader));
|
||||
|
||||
if((ATQA0 == 0x44 || ATQA0 == 0x04) && (SAK == 0x08 || SAK == 0x88 || SAK == 0x09)) {
|
||||
reader->type = MfClassicType1k;
|
||||
} else if((ATQA0 == 0x42 || ATQA0 == 0x02) && (SAK == 0x18)) {
|
||||
reader->type = MfClassicType4k;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
void mf_classic_reader_add_sector(
|
||||
MfClassicReader* reader,
|
||||
uint8_t sector,
|
||||
uint64_t key_a,
|
||||
uint64_t key_b) {
|
||||
furi_assert(reader);
|
||||
furi_assert(sector < MF_CLASSIC_SECTORS_MAX);
|
||||
furi_assert((key_a != MF_CLASSIC_NO_KEY) || (key_b != MF_CLASSIC_NO_KEY));
|
||||
|
||||
if(reader->sectors_to_read < MF_CLASSIC_SECTORS_MAX) {
|
||||
reader->sector_reader[reader->sectors_to_read].key_a = key_a;
|
||||
reader->sector_reader[reader->sectors_to_read].key_b = key_b;
|
||||
reader->sector_reader[reader->sectors_to_read].sector_num = sector;
|
||||
reader->sectors_to_read++;
|
||||
}
|
||||
}
|
||||
|
||||
void mf_classic_auth_init_context(MfClassicAuthContext* auth_ctx, uint8_t sector) {
|
||||
furi_assert(auth_ctx);
|
||||
auth_ctx->sector = sector;
|
||||
auth_ctx->key_a = MF_CLASSIC_NO_KEY;
|
||||
auth_ctx->key_b = MF_CLASSIC_NO_KEY;
|
||||
}
|
||||
|
||||
static bool mf_classic_auth(
|
||||
FuriHalNfcTxRxContext* tx_rx,
|
||||
uint32_t block,
|
||||
uint64_t key,
|
||||
MfClassicKey key_type,
|
||||
Crypto1* crypto) {
|
||||
bool auth_success = false;
|
||||
uint32_t cuid = 0;
|
||||
memset(tx_rx->tx_data, 0, sizeof(tx_rx->tx_data));
|
||||
memset(tx_rx->tx_parity, 0, sizeof(tx_rx->tx_parity));
|
||||
tx_rx->tx_rx_type = FuriHalNfcTxRxTypeDefault;
|
||||
|
||||
do {
|
||||
if(!furi_hal_nfc_activate_nfca(200, &cuid)) break;
|
||||
if(key_type == MfClassicKeyA) {
|
||||
tx_rx->tx_data[0] = MF_CLASSIC_AUTH_KEY_A_CMD;
|
||||
} else {
|
||||
tx_rx->tx_data[0] = MF_CLASSIC_AUTH_KEY_B_CMD;
|
||||
}
|
||||
tx_rx->tx_data[1] = block;
|
||||
tx_rx->tx_rx_type = FuriHalNfcTxRxTypeRxNoCrc;
|
||||
tx_rx->tx_bits = 2 * 8;
|
||||
if(!furi_hal_nfc_tx_rx(tx_rx, 6)) break;
|
||||
|
||||
uint32_t nt = (uint32_t)nfc_util_bytes2num(tx_rx->rx_data, 4);
|
||||
crypto1_init(crypto, key);
|
||||
crypto1_word(crypto, nt ^ cuid, 0);
|
||||
uint8_t nr[4] = {};
|
||||
nfc_util_num2bytes(prng_successor(DWT->CYCCNT, 32), 4, nr);
|
||||
for(uint8_t i = 0; i < 4; i++) {
|
||||
tx_rx->tx_data[i] = crypto1_byte(crypto, nr[i], 0) ^ nr[i];
|
||||
tx_rx->tx_parity[0] |=
|
||||
(((crypto1_filter(crypto->odd) ^ nfc_util_odd_parity8(nr[i])) & 0x01) << (7 - i));
|
||||
}
|
||||
nt = prng_successor(nt, 32);
|
||||
for(uint8_t i = 4; i < 8; i++) {
|
||||
nt = prng_successor(nt, 8);
|
||||
tx_rx->tx_data[i] = crypto1_byte(crypto, 0x00, 0) ^ (nt & 0xff);
|
||||
tx_rx->tx_parity[0] |=
|
||||
(((crypto1_filter(crypto->odd) ^ nfc_util_odd_parity8(nt & 0xff)) & 0x01)
|
||||
<< (7 - i));
|
||||
}
|
||||
tx_rx->tx_rx_type = FuriHalNfcTxRxTypeRaw;
|
||||
tx_rx->tx_bits = 8 * 8;
|
||||
if(!furi_hal_nfc_tx_rx(tx_rx, 6)) break;
|
||||
if(tx_rx->rx_bits == 32) {
|
||||
crypto1_word(crypto, 0, 0);
|
||||
auth_success = true;
|
||||
}
|
||||
} while(false);
|
||||
|
||||
return auth_success;
|
||||
}
|
||||
|
||||
bool mf_classic_authenticate(
|
||||
FuriHalNfcTxRxContext* tx_rx,
|
||||
uint8_t block_num,
|
||||
uint64_t key,
|
||||
MfClassicKey key_type) {
|
||||
furi_assert(tx_rx);
|
||||
|
||||
Crypto1 crypto = {};
|
||||
bool key_found = mf_classic_auth(tx_rx, block_num, key, key_type, &crypto);
|
||||
furi_hal_nfc_sleep();
|
||||
return key_found;
|
||||
}
|
||||
|
||||
bool mf_classic_auth_attempt(
|
||||
FuriHalNfcTxRxContext* tx_rx,
|
||||
MfClassicAuthContext* auth_ctx,
|
||||
uint64_t key) {
|
||||
furi_assert(tx_rx);
|
||||
furi_assert(auth_ctx);
|
||||
bool found_key = false;
|
||||
bool need_halt = (auth_ctx->key_a == MF_CLASSIC_NO_KEY) &&
|
||||
(auth_ctx->key_b == MF_CLASSIC_NO_KEY);
|
||||
|
||||
Crypto1 crypto;
|
||||
if(auth_ctx->key_a == MF_CLASSIC_NO_KEY) {
|
||||
// Try AUTH with key A
|
||||
if(mf_classic_auth(
|
||||
tx_rx,
|
||||
mf_classic_get_first_block_num_of_sector(auth_ctx->sector),
|
||||
key,
|
||||
MfClassicKeyA,
|
||||
&crypto)) {
|
||||
auth_ctx->key_a = key;
|
||||
found_key = true;
|
||||
}
|
||||
}
|
||||
|
||||
if(need_halt) {
|
||||
furi_hal_nfc_sleep();
|
||||
}
|
||||
|
||||
if(auth_ctx->key_b == MF_CLASSIC_NO_KEY) {
|
||||
// Try AUTH with key B
|
||||
if(mf_classic_auth(
|
||||
tx_rx,
|
||||
mf_classic_get_first_block_num_of_sector(auth_ctx->sector),
|
||||
key,
|
||||
MfClassicKeyB,
|
||||
&crypto)) {
|
||||
auth_ctx->key_b = key;
|
||||
found_key = true;
|
||||
}
|
||||
}
|
||||
|
||||
return found_key;
|
||||
}
|
||||
|
||||
bool mf_classic_read_block(
|
||||
FuriHalNfcTxRxContext* tx_rx,
|
||||
Crypto1* crypto,
|
||||
uint8_t block_num,
|
||||
MfClassicBlock* block) {
|
||||
furi_assert(tx_rx);
|
||||
furi_assert(crypto);
|
||||
furi_assert(block);
|
||||
|
||||
bool read_block_success = false;
|
||||
uint8_t plain_cmd[4] = {MF_CLASSIC_READ_SECT_CMD, block_num, 0x00, 0x00};
|
||||
nfca_append_crc16(plain_cmd, 2);
|
||||
memset(tx_rx->tx_data, 0, sizeof(tx_rx->tx_data));
|
||||
memset(tx_rx->tx_parity, 0, sizeof(tx_rx->tx_parity));
|
||||
|
||||
for(uint8_t i = 0; i < 4; i++) {
|
||||
tx_rx->tx_data[i] = crypto1_byte(crypto, 0x00, 0) ^ plain_cmd[i];
|
||||
tx_rx->tx_parity[0] |=
|
||||
((crypto1_filter(crypto->odd) ^ nfc_util_odd_parity8(plain_cmd[i])) & 0x01) << (7 - i);
|
||||
}
|
||||
tx_rx->tx_bits = 4 * 9;
|
||||
tx_rx->tx_rx_type = FuriHalNfcTxRxTypeRaw;
|
||||
|
||||
if(furi_hal_nfc_tx_rx(tx_rx, 50)) {
|
||||
if(tx_rx->rx_bits == 8 * (MF_CLASSIC_BLOCK_SIZE + 2)) {
|
||||
uint8_t block_received[MF_CLASSIC_BLOCK_SIZE + 2];
|
||||
for(uint8_t i = 0; i < MF_CLASSIC_BLOCK_SIZE + 2; i++) {
|
||||
block_received[i] = crypto1_byte(crypto, 0, 0) ^ tx_rx->rx_data[i];
|
||||
}
|
||||
uint16_t crc_calc = nfca_get_crc16(block_received, MF_CLASSIC_BLOCK_SIZE);
|
||||
uint16_t crc_received = (block_received[MF_CLASSIC_BLOCK_SIZE + 1] << 8) |
|
||||
block_received[MF_CLASSIC_BLOCK_SIZE];
|
||||
if(crc_received != crc_calc) {
|
||||
FURI_LOG_E(
|
||||
TAG,
|
||||
"Incorrect CRC while reading block %d. Expected %04X, Received %04X",
|
||||
block_num,
|
||||
crc_received,
|
||||
crc_calc);
|
||||
} else {
|
||||
memcpy(block->value, block_received, MF_CLASSIC_BLOCK_SIZE);
|
||||
read_block_success = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
return read_block_success;
|
||||
}
|
||||
|
||||
void mf_classic_read_sector(FuriHalNfcTxRxContext* tx_rx, MfClassicData* data, uint8_t sec_num) {
|
||||
furi_assert(tx_rx);
|
||||
furi_assert(data);
|
||||
|
||||
furi_hal_nfc_sleep();
|
||||
bool key_a_found = mf_classic_is_key_found(data, sec_num, MfClassicKeyA);
|
||||
bool key_b_found = mf_classic_is_key_found(data, sec_num, MfClassicKeyB);
|
||||
uint8_t start_block = mf_classic_get_first_block_num_of_sector(sec_num);
|
||||
uint8_t total_blocks = mf_classic_get_blocks_num_in_sector(sec_num);
|
||||
MfClassicBlock block_tmp = {};
|
||||
uint64_t key = 0;
|
||||
MfClassicSectorTrailer* sec_tr = mf_classic_get_sector_trailer_by_sector(data, sec_num);
|
||||
Crypto1 crypto = {};
|
||||
|
||||
uint8_t blocks_read = 0;
|
||||
do {
|
||||
if(!key_a_found) break;
|
||||
FURI_LOG_D(TAG, "Try to read blocks with key A");
|
||||
key = nfc_util_bytes2num(sec_tr->key_a, sizeof(sec_tr->key_a));
|
||||
if(!mf_classic_auth(tx_rx, start_block, key, MfClassicKeyA, &crypto)) break;
|
||||
for(size_t i = start_block; i < start_block + total_blocks; i++) {
|
||||
if(!mf_classic_is_block_read(data, i)) {
|
||||
if(mf_classic_read_block(tx_rx, &crypto, i, &block_tmp)) {
|
||||
mf_classic_set_block_read(data, i, &block_tmp);
|
||||
blocks_read++;
|
||||
}
|
||||
} else {
|
||||
blocks_read++;
|
||||
}
|
||||
}
|
||||
FURI_LOG_D(TAG, "Read %d blocks out of %d", blocks_read, total_blocks);
|
||||
} while(false);
|
||||
do {
|
||||
if(blocks_read == total_blocks) break;
|
||||
if(!key_b_found) break;
|
||||
FURI_LOG_D(TAG, "Try to read blocks with key B");
|
||||
key = nfc_util_bytes2num(sec_tr->key_b, sizeof(sec_tr->key_b));
|
||||
furi_hal_nfc_sleep();
|
||||
if(!mf_classic_auth(tx_rx, start_block, key, MfClassicKeyB, &crypto)) break;
|
||||
for(size_t i = start_block; i < start_block + total_blocks; i++) {
|
||||
if(!mf_classic_is_block_read(data, i)) {
|
||||
if(mf_classic_read_block(tx_rx, &crypto, i, &block_tmp)) {
|
||||
mf_classic_set_block_read(data, i, &block_tmp);
|
||||
blocks_read++;
|
||||
}
|
||||
} else {
|
||||
blocks_read++;
|
||||
}
|
||||
}
|
||||
FURI_LOG_D(TAG, "Read %d blocks out of %d", blocks_read, total_blocks);
|
||||
} while(false);
|
||||
}
|
||||
|
||||
static bool mf_classic_read_sector_with_reader(
|
||||
FuriHalNfcTxRxContext* tx_rx,
|
||||
Crypto1* crypto,
|
||||
MfClassicSectorReader* sector_reader,
|
||||
MfClassicSector* sector) {
|
||||
furi_assert(tx_rx);
|
||||
furi_assert(sector_reader);
|
||||
furi_assert(sector);
|
||||
|
||||
uint64_t key;
|
||||
MfClassicKey key_type;
|
||||
uint8_t first_block;
|
||||
bool sector_read = false;
|
||||
|
||||
furi_hal_nfc_sleep();
|
||||
do {
|
||||
// Activate card
|
||||
first_block = mf_classic_get_first_block_num_of_sector(sector_reader->sector_num);
|
||||
if(sector_reader->key_a != MF_CLASSIC_NO_KEY) {
|
||||
key = sector_reader->key_a;
|
||||
key_type = MfClassicKeyA;
|
||||
} else if(sector_reader->key_b != MF_CLASSIC_NO_KEY) {
|
||||
key = sector_reader->key_b;
|
||||
key_type = MfClassicKeyB;
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
|
||||
// Auth to first block in sector
|
||||
if(!mf_classic_auth(tx_rx, first_block, key, key_type, crypto)) break;
|
||||
sector->total_blocks = mf_classic_get_blocks_num_in_sector(sector_reader->sector_num);
|
||||
|
||||
// Read blocks
|
||||
for(uint8_t i = 0; i < sector->total_blocks; i++) {
|
||||
mf_classic_read_block(tx_rx, crypto, first_block + i, §or->block[i]);
|
||||
}
|
||||
// Save sector keys in last block
|
||||
if(sector_reader->key_a != MF_CLASSIC_NO_KEY) {
|
||||
nfc_util_num2bytes(
|
||||
sector_reader->key_a, 6, §or->block[sector->total_blocks - 1].value[0]);
|
||||
}
|
||||
if(sector_reader->key_b != MF_CLASSIC_NO_KEY) {
|
||||
nfc_util_num2bytes(
|
||||
sector_reader->key_b, 6, §or->block[sector->total_blocks - 1].value[10]);
|
||||
}
|
||||
|
||||
sector_read = true;
|
||||
} while(false);
|
||||
|
||||
return sector_read;
|
||||
}
|
||||
|
||||
uint8_t mf_classic_read_card(
|
||||
FuriHalNfcTxRxContext* tx_rx,
|
||||
MfClassicReader* reader,
|
||||
MfClassicData* data) {
|
||||
furi_assert(tx_rx);
|
||||
furi_assert(reader);
|
||||
furi_assert(data);
|
||||
|
||||
uint8_t sectors_read = 0;
|
||||
data->type = reader->type;
|
||||
data->key_a_mask = 0;
|
||||
data->key_b_mask = 0;
|
||||
MfClassicSector temp_sector = {};
|
||||
for(uint8_t i = 0; i < reader->sectors_to_read; i++) {
|
||||
if(mf_classic_read_sector_with_reader(
|
||||
tx_rx, &reader->crypto, &reader->sector_reader[i], &temp_sector)) {
|
||||
uint8_t first_block =
|
||||
mf_classic_get_first_block_num_of_sector(reader->sector_reader[i].sector_num);
|
||||
for(uint8_t j = 0; j < temp_sector.total_blocks; j++) {
|
||||
mf_classic_set_block_read(data, first_block + j, &temp_sector.block[j]);
|
||||
}
|
||||
if(reader->sector_reader[i].key_a != MF_CLASSIC_NO_KEY) {
|
||||
mf_classic_set_key_found(
|
||||
data,
|
||||
reader->sector_reader[i].sector_num,
|
||||
MfClassicKeyA,
|
||||
reader->sector_reader[i].key_a);
|
||||
}
|
||||
if(reader->sector_reader[i].key_b != MF_CLASSIC_NO_KEY) {
|
||||
mf_classic_set_key_found(
|
||||
data,
|
||||
reader->sector_reader[i].sector_num,
|
||||
MfClassicKeyB,
|
||||
reader->sector_reader[i].key_b);
|
||||
}
|
||||
sectors_read++;
|
||||
}
|
||||
}
|
||||
|
||||
return sectors_read;
|
||||
}
|
||||
|
||||
uint8_t mf_classic_update_card(FuriHalNfcTxRxContext* tx_rx, MfClassicData* data) {
|
||||
furi_assert(tx_rx);
|
||||
furi_assert(data);
|
||||
|
||||
uint8_t sectors_read = 0;
|
||||
Crypto1 crypto = {};
|
||||
uint8_t total_sectors = mf_classic_get_total_sectors_num(data->type);
|
||||
uint64_t key_a = 0;
|
||||
uint64_t key_b = 0;
|
||||
MfClassicSectorReader sec_reader = {};
|
||||
MfClassicSector temp_sector = {};
|
||||
|
||||
for(size_t i = 0; i < total_sectors; i++) {
|
||||
MfClassicSectorTrailer* sec_tr = mf_classic_get_sector_trailer_by_sector(data, i);
|
||||
// Load key A
|
||||
if(mf_classic_is_key_found(data, i, MfClassicKeyA)) {
|
||||
sec_reader.key_a = nfc_util_bytes2num(sec_tr->key_a, 6);
|
||||
} else {
|
||||
sec_reader.key_a = MF_CLASSIC_NO_KEY;
|
||||
}
|
||||
// Load key B
|
||||
if(mf_classic_is_key_found(data, i, MfClassicKeyB)) {
|
||||
sec_reader.key_b = nfc_util_bytes2num(sec_tr->key_b, 6);
|
||||
} else {
|
||||
sec_reader.key_b = MF_CLASSIC_NO_KEY;
|
||||
}
|
||||
if((key_a != MF_CLASSIC_NO_KEY) || (key_b != MF_CLASSIC_NO_KEY)) {
|
||||
sec_reader.sector_num = i;
|
||||
if(mf_classic_read_sector_with_reader(tx_rx, &crypto, &sec_reader, &temp_sector)) {
|
||||
uint8_t first_block = mf_classic_get_first_block_num_of_sector(i);
|
||||
for(uint8_t j = 0; j < temp_sector.total_blocks; j++) {
|
||||
mf_classic_set_block_read(data, first_block + j, &temp_sector.block[j]);
|
||||
}
|
||||
sectors_read++;
|
||||
}
|
||||
}
|
||||
}
|
||||
return sectors_read;
|
||||
}
|
||||
|
||||
void mf_crypto1_decrypt(
|
||||
Crypto1* crypto,
|
||||
uint8_t* encrypted_data,
|
||||
uint16_t encrypted_data_bits,
|
||||
uint8_t* decrypted_data) {
|
||||
if(encrypted_data_bits < 8) {
|
||||
uint8_t decrypted_byte = 0;
|
||||
decrypted_byte |= (crypto1_bit(crypto, 0, 0) ^ FURI_BIT(encrypted_data[0], 0)) << 0;
|
||||
decrypted_byte |= (crypto1_bit(crypto, 0, 0) ^ FURI_BIT(encrypted_data[0], 1)) << 1;
|
||||
decrypted_byte |= (crypto1_bit(crypto, 0, 0) ^ FURI_BIT(encrypted_data[0], 2)) << 2;
|
||||
decrypted_byte |= (crypto1_bit(crypto, 0, 0) ^ FURI_BIT(encrypted_data[0], 3)) << 3;
|
||||
decrypted_data[0] = decrypted_byte;
|
||||
} else {
|
||||
for(size_t i = 0; i < encrypted_data_bits / 8; i++) {
|
||||
decrypted_data[i] = crypto1_byte(crypto, 0, 0) ^ encrypted_data[i];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void mf_crypto1_encrypt(
|
||||
Crypto1* crypto,
|
||||
uint8_t* keystream,
|
||||
uint8_t* plain_data,
|
||||
uint16_t plain_data_bits,
|
||||
uint8_t* encrypted_data,
|
||||
uint8_t* encrypted_parity) {
|
||||
if(plain_data_bits < 8) {
|
||||
encrypted_data[0] = 0;
|
||||
for(size_t i = 0; i < plain_data_bits; i++) {
|
||||
encrypted_data[0] |= (crypto1_bit(crypto, 0, 0) ^ FURI_BIT(plain_data[0], i)) << i;
|
||||
}
|
||||
} else {
|
||||
memset(encrypted_parity, 0, plain_data_bits / 8 + 1);
|
||||
for(uint8_t i = 0; i < plain_data_bits / 8; i++) {
|
||||
encrypted_data[i] = crypto1_byte(crypto, keystream ? keystream[i] : 0, 0) ^
|
||||
plain_data[i];
|
||||
encrypted_parity[i / 8] |=
|
||||
(((crypto1_filter(crypto->odd) ^ nfc_util_odd_parity8(plain_data[i])) & 0x01)
|
||||
<< (7 - (i & 0x0007)));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
bool mf_classic_emulator(MfClassicEmulator* emulator, FuriHalNfcTxRxContext* tx_rx) {
|
||||
furi_assert(emulator);
|
||||
furi_assert(tx_rx);
|
||||
bool command_processed = false;
|
||||
bool is_encrypted = false;
|
||||
uint8_t plain_data[MF_CLASSIC_MAX_DATA_SIZE];
|
||||
MfClassicKey access_key = MfClassicKeyA;
|
||||
|
||||
// Read command
|
||||
while(!command_processed) {
|
||||
if(!is_encrypted) {
|
||||
memcpy(plain_data, tx_rx->rx_data, tx_rx->rx_bits / 8);
|
||||
} else {
|
||||
if(!furi_hal_nfc_tx_rx(tx_rx, 300)) {
|
||||
FURI_LOG_D(
|
||||
TAG,
|
||||
"Error in tx rx. Tx :%d bits, Rx: %d bits",
|
||||
tx_rx->tx_bits,
|
||||
tx_rx->rx_bits);
|
||||
break;
|
||||
}
|
||||
mf_crypto1_decrypt(&emulator->crypto, tx_rx->rx_data, tx_rx->rx_bits, plain_data);
|
||||
}
|
||||
|
||||
if(plain_data[0] == 0x50 && plain_data[1] == 0x00) {
|
||||
FURI_LOG_T(TAG, "Halt received");
|
||||
furi_hal_nfc_listen_sleep();
|
||||
command_processed = true;
|
||||
break;
|
||||
} else if(plain_data[0] == 0x60 || plain_data[0] == 0x61) {
|
||||
uint8_t block = plain_data[1];
|
||||
uint64_t key = 0;
|
||||
uint8_t sector_trailer_block = mf_classic_get_sector_trailer_num_by_block(block);
|
||||
MfClassicSectorTrailer* sector_trailer =
|
||||
(MfClassicSectorTrailer*)emulator->data.block[sector_trailer_block].value;
|
||||
if(plain_data[0] == 0x60) {
|
||||
key = nfc_util_bytes2num(sector_trailer->key_a, 6);
|
||||
access_key = MfClassicKeyA;
|
||||
} else {
|
||||
key = nfc_util_bytes2num(sector_trailer->key_b, 6);
|
||||
access_key = MfClassicKeyB;
|
||||
}
|
||||
|
||||
uint32_t nonce = prng_successor(DWT->CYCCNT, 32);
|
||||
uint8_t nt[4];
|
||||
uint8_t nt_keystream[4];
|
||||
nfc_util_num2bytes(nonce, 4, nt);
|
||||
nfc_util_num2bytes(nonce ^ emulator->cuid, 4, nt_keystream);
|
||||
crypto1_init(&emulator->crypto, key);
|
||||
if(!is_encrypted) {
|
||||
crypto1_word(&emulator->crypto, emulator->cuid ^ nonce, 0);
|
||||
memcpy(tx_rx->tx_data, nt, sizeof(nt));
|
||||
tx_rx->tx_parity[0] = 0;
|
||||
for(size_t i = 0; i < sizeof(nt); i++) {
|
||||
tx_rx->tx_parity[0] |= nfc_util_odd_parity8(nt[i]) << (7 - i);
|
||||
}
|
||||
tx_rx->tx_bits = sizeof(nt) * 8;
|
||||
tx_rx->tx_rx_type = FuriHalNfcTxRxTransparent;
|
||||
} else {
|
||||
mf_crypto1_encrypt(
|
||||
&emulator->crypto,
|
||||
nt_keystream,
|
||||
nt,
|
||||
sizeof(nt) * 8,
|
||||
tx_rx->tx_data,
|
||||
tx_rx->tx_parity);
|
||||
tx_rx->tx_bits = sizeof(nt) * 8;
|
||||
tx_rx->tx_rx_type = FuriHalNfcTxRxTransparent;
|
||||
}
|
||||
if(!furi_hal_nfc_tx_rx(tx_rx, 500)) {
|
||||
FURI_LOG_E(TAG, "Error in NT exchange");
|
||||
command_processed = true;
|
||||
break;
|
||||
}
|
||||
|
||||
if(tx_rx->rx_bits != 64) {
|
||||
FURI_LOG_W(TAG, "Incorrect nr + ar");
|
||||
command_processed = true;
|
||||
break;
|
||||
}
|
||||
|
||||
uint32_t nr = nfc_util_bytes2num(tx_rx->rx_data, 4);
|
||||
uint32_t ar = nfc_util_bytes2num(&tx_rx->rx_data[4], 4);
|
||||
|
||||
FURI_LOG_D(
|
||||
TAG,
|
||||
"%08x key%c block %d nt/nr/ar: %08x %08x %08x",
|
||||
emulator->cuid,
|
||||
access_key == MfClassicKeyA ? 'A' : 'B',
|
||||
sector_trailer_block,
|
||||
nonce,
|
||||
nr,
|
||||
ar);
|
||||
|
||||
crypto1_word(&emulator->crypto, nr, 1);
|
||||
uint32_t cardRr = ar ^ crypto1_word(&emulator->crypto, 0, 0);
|
||||
if(cardRr != prng_successor(nonce, 64)) {
|
||||
FURI_LOG_T(TAG, "Wrong AUTH! %08X != %08X", cardRr, prng_successor(nonce, 64));
|
||||
// Don't send NACK, as tag don't send it
|
||||
command_processed = true;
|
||||
break;
|
||||
}
|
||||
|
||||
uint32_t ans = prng_successor(nonce, 96);
|
||||
uint8_t responce[4] = {};
|
||||
nfc_util_num2bytes(ans, 4, responce);
|
||||
mf_crypto1_encrypt(
|
||||
&emulator->crypto,
|
||||
NULL,
|
||||
responce,
|
||||
sizeof(responce) * 8,
|
||||
tx_rx->tx_data,
|
||||
tx_rx->tx_parity);
|
||||
tx_rx->tx_bits = sizeof(responce) * 8;
|
||||
tx_rx->tx_rx_type = FuriHalNfcTxRxTransparent;
|
||||
|
||||
is_encrypted = true;
|
||||
} else if(is_encrypted && plain_data[0] == 0x30) {
|
||||
uint8_t block = plain_data[1];
|
||||
uint8_t block_data[18] = {};
|
||||
memcpy(block_data, emulator->data.block[block].value, MF_CLASSIC_BLOCK_SIZE);
|
||||
if(mf_classic_is_sector_trailer(block)) {
|
||||
if(!mf_classic_is_allowed_access(
|
||||
emulator, block, access_key, MfClassicActionKeyARead)) {
|
||||
memset(block_data, 0, 6);
|
||||
}
|
||||
if(!mf_classic_is_allowed_access(
|
||||
emulator, block, access_key, MfClassicActionKeyBRead)) {
|
||||
memset(&block_data[10], 0, 6);
|
||||
}
|
||||
if(!mf_classic_is_allowed_access(
|
||||
emulator, block, access_key, MfClassicActionACRead)) {
|
||||
memset(&block_data[6], 0, 4);
|
||||
}
|
||||
} else {
|
||||
if(!mf_classic_is_allowed_access(
|
||||
emulator, block, access_key, MfClassicActionDataRead)) {
|
||||
memset(block_data, 0, 16);
|
||||
}
|
||||
}
|
||||
nfca_append_crc16(block_data, 16);
|
||||
|
||||
mf_crypto1_encrypt(
|
||||
&emulator->crypto,
|
||||
NULL,
|
||||
block_data,
|
||||
sizeof(block_data) * 8,
|
||||
tx_rx->tx_data,
|
||||
tx_rx->tx_parity);
|
||||
tx_rx->tx_bits = 18 * 8;
|
||||
tx_rx->tx_rx_type = FuriHalNfcTxRxTransparent;
|
||||
} else if(is_encrypted && plain_data[0] == 0xA0) {
|
||||
uint8_t block = plain_data[1];
|
||||
if(block > mf_classic_get_total_block_num(emulator->data.type)) {
|
||||
break;
|
||||
}
|
||||
// Send ACK
|
||||
uint8_t ack = 0x0A;
|
||||
mf_crypto1_encrypt(&emulator->crypto, NULL, &ack, 4, tx_rx->tx_data, tx_rx->tx_parity);
|
||||
tx_rx->tx_rx_type = FuriHalNfcTxRxTransparent;
|
||||
tx_rx->tx_bits = 4;
|
||||
|
||||
if(!furi_hal_nfc_tx_rx(tx_rx, 300)) break;
|
||||
if(tx_rx->rx_bits != 18 * 8) break;
|
||||
|
||||
mf_crypto1_decrypt(&emulator->crypto, tx_rx->rx_data, tx_rx->rx_bits, plain_data);
|
||||
uint8_t block_data[16] = {};
|
||||
memcpy(block_data, emulator->data.block[block].value, MF_CLASSIC_BLOCK_SIZE);
|
||||
if(mf_classic_is_sector_trailer(block)) {
|
||||
if(mf_classic_is_allowed_access(
|
||||
emulator, block, access_key, MfClassicActionKeyAWrite)) {
|
||||
memcpy(block_data, plain_data, 6);
|
||||
}
|
||||
if(mf_classic_is_allowed_access(
|
||||
emulator, block, access_key, MfClassicActionKeyBWrite)) {
|
||||
memcpy(&block_data[10], &plain_data[10], 6);
|
||||
}
|
||||
if(mf_classic_is_allowed_access(
|
||||
emulator, block, access_key, MfClassicActionACWrite)) {
|
||||
memcpy(&block_data[6], &plain_data[6], 4);
|
||||
}
|
||||
} else {
|
||||
if(mf_classic_is_allowed_access(
|
||||
emulator, block, access_key, MfClassicActionDataWrite)) {
|
||||
memcpy(block_data, plain_data, MF_CLASSIC_BLOCK_SIZE);
|
||||
}
|
||||
}
|
||||
if(memcmp(block_data, emulator->data.block[block].value, MF_CLASSIC_BLOCK_SIZE)) {
|
||||
memcpy(emulator->data.block[block].value, block_data, MF_CLASSIC_BLOCK_SIZE);
|
||||
emulator->data_changed = true;
|
||||
}
|
||||
// Send ACK
|
||||
ack = 0x0A;
|
||||
mf_crypto1_encrypt(&emulator->crypto, NULL, &ack, 4, tx_rx->tx_data, tx_rx->tx_parity);
|
||||
tx_rx->tx_rx_type = FuriHalNfcTxRxTransparent;
|
||||
tx_rx->tx_bits = 4;
|
||||
} else {
|
||||
// Unknown command
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if(!command_processed) {
|
||||
// Send NACK
|
||||
uint8_t nack = 0x04;
|
||||
if(is_encrypted) {
|
||||
mf_crypto1_encrypt(
|
||||
&emulator->crypto, NULL, &nack, 4, tx_rx->tx_data, tx_rx->tx_parity);
|
||||
} else {
|
||||
tx_rx->tx_data[0] = nack;
|
||||
}
|
||||
tx_rx->tx_rx_type = FuriHalNfcTxRxTransparent;
|
||||
tx_rx->tx_bits = 4;
|
||||
furi_hal_nfc_tx_rx(tx_rx, 300);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
145
lib/nfc/protocols/mifare_classic.h
Normal file
145
lib/nfc/protocols/mifare_classic.h
Normal file
@@ -0,0 +1,145 @@
|
||||
#pragma once
|
||||
|
||||
#include <furi_hal_nfc.h>
|
||||
|
||||
#include "crypto1.h"
|
||||
|
||||
#define MF_CLASSIC_BLOCK_SIZE (16)
|
||||
#define MF_CLASSIC_TOTAL_BLOCKS_MAX (256)
|
||||
#define MF_CLASSIC_1K_TOTAL_SECTORS_NUM (16)
|
||||
#define MF_CLASSIC_4K_TOTAL_SECTORS_NUM (40)
|
||||
|
||||
#define MF_CLASSIC_SECTORS_MAX (40)
|
||||
#define MF_CLASSIC_BLOCKS_IN_SECTOR_MAX (16)
|
||||
|
||||
#define MF_CLASSIC_NO_KEY (0xFFFFFFFFFFFFFFFF)
|
||||
#define MF_CLASSIC_MAX_DATA_SIZE (16)
|
||||
#define MF_CLASSIC_KEY_SIZE (6)
|
||||
#define MF_CLASSIC_ACCESS_BYTES_SIZE (4)
|
||||
|
||||
typedef enum {
|
||||
MfClassicType1k,
|
||||
MfClassicType4k,
|
||||
} MfClassicType;
|
||||
|
||||
typedef enum {
|
||||
MfClassicKeyA,
|
||||
MfClassicKeyB,
|
||||
} MfClassicKey;
|
||||
|
||||
typedef struct {
|
||||
uint8_t value[MF_CLASSIC_BLOCK_SIZE];
|
||||
} MfClassicBlock;
|
||||
|
||||
typedef struct {
|
||||
uint8_t key_a[MF_CLASSIC_KEY_SIZE];
|
||||
uint8_t access_bits[MF_CLASSIC_ACCESS_BYTES_SIZE];
|
||||
uint8_t key_b[MF_CLASSIC_KEY_SIZE];
|
||||
} MfClassicSectorTrailer;
|
||||
|
||||
typedef struct {
|
||||
uint8_t total_blocks;
|
||||
MfClassicBlock block[MF_CLASSIC_BLOCKS_IN_SECTOR_MAX];
|
||||
} MfClassicSector;
|
||||
|
||||
typedef struct {
|
||||
MfClassicType type;
|
||||
uint32_t block_read_mask[MF_CLASSIC_TOTAL_BLOCKS_MAX / 32];
|
||||
uint64_t key_a_mask;
|
||||
uint64_t key_b_mask;
|
||||
MfClassicBlock block[MF_CLASSIC_TOTAL_BLOCKS_MAX];
|
||||
} MfClassicData;
|
||||
|
||||
typedef struct {
|
||||
uint8_t sector;
|
||||
uint64_t key_a;
|
||||
uint64_t key_b;
|
||||
} MfClassicAuthContext;
|
||||
|
||||
typedef struct {
|
||||
uint8_t sector_num;
|
||||
uint64_t key_a;
|
||||
uint64_t key_b;
|
||||
} MfClassicSectorReader;
|
||||
|
||||
typedef struct {
|
||||
MfClassicType type;
|
||||
Crypto1 crypto;
|
||||
uint8_t sectors_to_read;
|
||||
MfClassicSectorReader sector_reader[MF_CLASSIC_SECTORS_MAX];
|
||||
} MfClassicReader;
|
||||
|
||||
typedef struct {
|
||||
uint32_t cuid;
|
||||
Crypto1 crypto;
|
||||
MfClassicData data;
|
||||
bool data_changed;
|
||||
} MfClassicEmulator;
|
||||
|
||||
const char* mf_classic_get_type_str(MfClassicType type);
|
||||
|
||||
bool mf_classic_check_card_type(uint8_t ATQA0, uint8_t ATQA1, uint8_t SAK);
|
||||
|
||||
MfClassicType mf_classic_get_classic_type(int8_t ATQA0, uint8_t ATQA1, uint8_t SAK);
|
||||
|
||||
bool mf_classic_get_type(uint8_t ATQA0, uint8_t ATQA1, uint8_t SAK, MfClassicReader* reader);
|
||||
|
||||
uint8_t mf_classic_get_total_sectors_num(MfClassicType type);
|
||||
|
||||
uint8_t mf_classic_get_sector_trailer_block_num_by_sector(uint8_t sector);
|
||||
|
||||
bool mf_classic_is_sector_trailer(uint8_t block);
|
||||
|
||||
uint8_t mf_classic_get_sector_by_block(uint8_t block);
|
||||
|
||||
bool mf_classic_is_key_found(MfClassicData* data, uint8_t sector_num, MfClassicKey key_type);
|
||||
|
||||
void mf_classic_set_key_found(
|
||||
MfClassicData* data,
|
||||
uint8_t sector_num,
|
||||
MfClassicKey key_type,
|
||||
uint64_t key);
|
||||
|
||||
bool mf_classic_is_block_read(MfClassicData* data, uint8_t block_num);
|
||||
|
||||
void mf_classic_set_block_read(MfClassicData* data, uint8_t block_num, MfClassicBlock* block_data);
|
||||
|
||||
bool mf_classic_is_sector_read(MfClassicData* data, uint8_t sector_num);
|
||||
|
||||
void mf_classic_get_read_sectors_and_keys(
|
||||
MfClassicData* data,
|
||||
uint8_t* sectors_read,
|
||||
uint8_t* keys_found);
|
||||
|
||||
MfClassicSectorTrailer*
|
||||
mf_classic_get_sector_trailer_by_sector(MfClassicData* data, uint8_t sector);
|
||||
|
||||
void mf_classic_auth_init_context(MfClassicAuthContext* auth_ctx, uint8_t sector);
|
||||
|
||||
bool mf_classic_authenticate(
|
||||
FuriHalNfcTxRxContext* tx_rx,
|
||||
uint8_t block_num,
|
||||
uint64_t key,
|
||||
MfClassicKey key_type);
|
||||
|
||||
bool mf_classic_auth_attempt(
|
||||
FuriHalNfcTxRxContext* tx_rx,
|
||||
MfClassicAuthContext* auth_ctx,
|
||||
uint64_t key);
|
||||
|
||||
void mf_classic_reader_add_sector(
|
||||
MfClassicReader* reader,
|
||||
uint8_t sector,
|
||||
uint64_t key_a,
|
||||
uint64_t key_b);
|
||||
|
||||
void mf_classic_read_sector(FuriHalNfcTxRxContext* tx_rx, MfClassicData* data, uint8_t sec_num);
|
||||
|
||||
uint8_t mf_classic_read_card(
|
||||
FuriHalNfcTxRxContext* tx_rx,
|
||||
MfClassicReader* reader,
|
||||
MfClassicData* data);
|
||||
|
||||
uint8_t mf_classic_update_card(FuriHalNfcTxRxContext* tx_rx, MfClassicData* data);
|
||||
|
||||
bool mf_classic_emulator(MfClassicEmulator* emulator, FuriHalNfcTxRxContext* tx_rx);
|
||||
17
lib/nfc/protocols/mifare_common.c
Normal file
17
lib/nfc/protocols/mifare_common.c
Normal file
@@ -0,0 +1,17 @@
|
||||
#include "mifare_common.h"
|
||||
|
||||
MifareType mifare_common_get_type(uint8_t ATQA0, uint8_t ATQA1, uint8_t SAK) {
|
||||
MifareType type = MifareTypeUnknown;
|
||||
|
||||
if((ATQA0 == 0x44) && (ATQA1 == 0x00) && (SAK == 0x00)) {
|
||||
type = MifareTypeUltralight;
|
||||
} else if(
|
||||
((ATQA0 == 0x44 || ATQA0 == 0x04) && (SAK == 0x08 || SAK == 0x88 || SAK == 0x09)) ||
|
||||
((ATQA0 == 0x42 || ATQA0 == 0x02) && (SAK == 0x18))) {
|
||||
type = MifareTypeClassic;
|
||||
} else if(ATQA0 == 0x44 && ATQA1 == 0x03 && SAK == 0x20) {
|
||||
type = MifareTypeDesfire;
|
||||
}
|
||||
|
||||
return type;
|
||||
}
|
||||
12
lib/nfc/protocols/mifare_common.h
Normal file
12
lib/nfc/protocols/mifare_common.h
Normal file
@@ -0,0 +1,12 @@
|
||||
#pragma once
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
typedef enum {
|
||||
MifareTypeUnknown,
|
||||
MifareTypeUltralight,
|
||||
MifareTypeClassic,
|
||||
MifareTypeDesfire,
|
||||
} MifareType;
|
||||
|
||||
MifareType mifare_common_get_type(uint8_t ATQA0, uint8_t ATQA1, uint8_t SAK);
|
||||
623
lib/nfc/protocols/mifare_desfire.c
Normal file
623
lib/nfc/protocols/mifare_desfire.c
Normal file
@@ -0,0 +1,623 @@
|
||||
#include "mifare_desfire.h"
|
||||
#include <furi.h>
|
||||
#include <furi_hal_nfc.h>
|
||||
|
||||
#define TAG "MifareDESFire"
|
||||
|
||||
void mf_df_clear(MifareDesfireData* data) {
|
||||
free(data->free_memory);
|
||||
if(data->master_key_settings) {
|
||||
MifareDesfireKeyVersion* key_version = data->master_key_settings->key_version_head;
|
||||
while(key_version) {
|
||||
MifareDesfireKeyVersion* next_key_version = key_version->next;
|
||||
free(key_version);
|
||||
key_version = next_key_version;
|
||||
}
|
||||
}
|
||||
free(data->master_key_settings);
|
||||
MifareDesfireApplication* app = data->app_head;
|
||||
while(app) {
|
||||
MifareDesfireApplication* next_app = app->next;
|
||||
if(app->key_settings) {
|
||||
MifareDesfireKeyVersion* key_version = app->key_settings->key_version_head;
|
||||
while(key_version) {
|
||||
MifareDesfireKeyVersion* next_key_version = key_version->next;
|
||||
free(key_version);
|
||||
key_version = next_key_version;
|
||||
}
|
||||
}
|
||||
free(app->key_settings);
|
||||
MifareDesfireFile* file = app->file_head;
|
||||
while(file) {
|
||||
MifareDesfireFile* next_file = file->next;
|
||||
free(file->contents);
|
||||
free(file);
|
||||
file = next_file;
|
||||
}
|
||||
free(app);
|
||||
app = next_app;
|
||||
}
|
||||
data->free_memory = NULL;
|
||||
data->master_key_settings = NULL;
|
||||
data->app_head = NULL;
|
||||
}
|
||||
|
||||
void mf_df_cat_data(MifareDesfireData* data, string_t out) {
|
||||
mf_df_cat_card_info(data, out);
|
||||
for(MifareDesfireApplication* app = data->app_head; app; app = app->next) {
|
||||
mf_df_cat_application(app, out);
|
||||
}
|
||||
}
|
||||
|
||||
void mf_df_cat_card_info(MifareDesfireData* data, string_t out) {
|
||||
mf_df_cat_version(&data->version, out);
|
||||
if(data->free_memory) {
|
||||
mf_df_cat_free_mem(data->free_memory, out);
|
||||
}
|
||||
if(data->master_key_settings) {
|
||||
mf_df_cat_key_settings(data->master_key_settings, out);
|
||||
}
|
||||
}
|
||||
|
||||
void mf_df_cat_version(MifareDesfireVersion* version, string_t out) {
|
||||
string_cat_printf(
|
||||
out,
|
||||
"%02x:%02x:%02x:%02x:%02x:%02x:%02x\n",
|
||||
version->uid[0],
|
||||
version->uid[1],
|
||||
version->uid[2],
|
||||
version->uid[3],
|
||||
version->uid[4],
|
||||
version->uid[5],
|
||||
version->uid[6]);
|
||||
string_cat_printf(
|
||||
out,
|
||||
"hw %02x type %02x sub %02x\n"
|
||||
" maj %02x min %02x\n"
|
||||
" size %02x proto %02x\n",
|
||||
version->hw_vendor,
|
||||
version->hw_type,
|
||||
version->hw_subtype,
|
||||
version->hw_major,
|
||||
version->hw_minor,
|
||||
version->hw_storage,
|
||||
version->hw_proto);
|
||||
string_cat_printf(
|
||||
out,
|
||||
"sw %02x type %02x sub %02x\n"
|
||||
" maj %02x min %02x\n"
|
||||
" size %02x proto %02x\n",
|
||||
version->sw_vendor,
|
||||
version->sw_type,
|
||||
version->sw_subtype,
|
||||
version->sw_major,
|
||||
version->sw_minor,
|
||||
version->sw_storage,
|
||||
version->sw_proto);
|
||||
string_cat_printf(
|
||||
out,
|
||||
"batch %02x:%02x:%02x:%02x:%02x\n"
|
||||
"week %d year %d\n",
|
||||
version->batch[0],
|
||||
version->batch[1],
|
||||
version->batch[2],
|
||||
version->batch[3],
|
||||
version->batch[4],
|
||||
version->prod_week,
|
||||
version->prod_year);
|
||||
}
|
||||
|
||||
void mf_df_cat_free_mem(MifareDesfireFreeMemory* free_mem, string_t out) {
|
||||
string_cat_printf(out, "freeMem %d\n", free_mem->bytes);
|
||||
}
|
||||
|
||||
void mf_df_cat_key_settings(MifareDesfireKeySettings* ks, string_t out) {
|
||||
string_cat_printf(out, "changeKeyID %d\n", ks->change_key_id);
|
||||
string_cat_printf(out, "configChangeable %d\n", ks->config_changeable);
|
||||
string_cat_printf(out, "freeCreateDelete %d\n", ks->free_create_delete);
|
||||
string_cat_printf(out, "freeDirectoryList %d\n", ks->free_directory_list);
|
||||
string_cat_printf(out, "masterChangeable %d\n", ks->master_key_changeable);
|
||||
if(ks->flags) {
|
||||
string_cat_printf(out, "flags %d\n", ks->flags);
|
||||
}
|
||||
string_cat_printf(out, "maxKeys %d\n", ks->max_keys);
|
||||
for(MifareDesfireKeyVersion* kv = ks->key_version_head; kv; kv = kv->next) {
|
||||
string_cat_printf(out, "key %d version %d\n", kv->id, kv->version);
|
||||
}
|
||||
}
|
||||
|
||||
void mf_df_cat_application_info(MifareDesfireApplication* app, string_t out) {
|
||||
string_cat_printf(out, "Application %02x%02x%02x\n", app->id[0], app->id[1], app->id[2]);
|
||||
if(app->key_settings) {
|
||||
mf_df_cat_key_settings(app->key_settings, out);
|
||||
}
|
||||
}
|
||||
|
||||
void mf_df_cat_application(MifareDesfireApplication* app, string_t out) {
|
||||
mf_df_cat_application_info(app, out);
|
||||
for(MifareDesfireFile* file = app->file_head; file; file = file->next) {
|
||||
mf_df_cat_file(file, out);
|
||||
}
|
||||
}
|
||||
|
||||
void mf_df_cat_file(MifareDesfireFile* file, string_t out) {
|
||||
char* type = "unknown";
|
||||
switch(file->type) {
|
||||
case MifareDesfireFileTypeStandard:
|
||||
type = "standard";
|
||||
break;
|
||||
case MifareDesfireFileTypeBackup:
|
||||
type = "backup";
|
||||
break;
|
||||
case MifareDesfireFileTypeValue:
|
||||
type = "value";
|
||||
break;
|
||||
case MifareDesfireFileTypeLinearRecord:
|
||||
type = "linear";
|
||||
break;
|
||||
case MifareDesfireFileTypeCyclicRecord:
|
||||
type = "cyclic";
|
||||
break;
|
||||
}
|
||||
char* comm = "unknown";
|
||||
switch(file->comm) {
|
||||
case MifareDesfireFileCommunicationSettingsPlaintext:
|
||||
comm = "plain";
|
||||
break;
|
||||
case MifareDesfireFileCommunicationSettingsAuthenticated:
|
||||
comm = "auth";
|
||||
break;
|
||||
case MifareDesfireFileCommunicationSettingsEnciphered:
|
||||
comm = "enciphered";
|
||||
break;
|
||||
}
|
||||
string_cat_printf(out, "File %d\n", file->id);
|
||||
string_cat_printf(out, "%s %s\n", type, comm);
|
||||
string_cat_printf(
|
||||
out,
|
||||
"r %d w %d rw %d c %d\n",
|
||||
file->access_rights >> 12 & 0xF,
|
||||
file->access_rights >> 8 & 0xF,
|
||||
file->access_rights >> 4 & 0xF,
|
||||
file->access_rights & 0xF);
|
||||
uint16_t size = 0;
|
||||
uint16_t num = 1;
|
||||
switch(file->type) {
|
||||
case MifareDesfireFileTypeStandard:
|
||||
case MifareDesfireFileTypeBackup:
|
||||
size = file->settings.data.size;
|
||||
string_cat_printf(out, "size %d\n", size);
|
||||
break;
|
||||
case MifareDesfireFileTypeValue:
|
||||
size = 4;
|
||||
string_cat_printf(
|
||||
out, "lo %d hi %d\n", file->settings.value.lo_limit, file->settings.value.hi_limit);
|
||||
string_cat_printf(
|
||||
out,
|
||||
"limit %d enabled %d\n",
|
||||
file->settings.value.limited_credit_value,
|
||||
file->settings.value.limited_credit_enabled);
|
||||
break;
|
||||
case MifareDesfireFileTypeLinearRecord:
|
||||
case MifareDesfireFileTypeCyclicRecord:
|
||||
size = file->settings.record.size;
|
||||
num = file->settings.record.cur;
|
||||
string_cat_printf(out, "size %d\n", size);
|
||||
string_cat_printf(out, "num %d max %d\n", num, file->settings.record.max);
|
||||
break;
|
||||
}
|
||||
uint8_t* data = file->contents;
|
||||
if(data) {
|
||||
for(int rec = 0; rec < num; rec++) {
|
||||
for(int ch = 0; ch < size; ch++) {
|
||||
string_cat_printf(out, "%02x", data[rec * size + ch]);
|
||||
}
|
||||
string_cat_printf(out, " \n");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
bool mf_df_check_card_type(uint8_t ATQA0, uint8_t ATQA1, uint8_t SAK) {
|
||||
return ATQA0 == 0x44 && ATQA1 == 0x03 && SAK == 0x20;
|
||||
}
|
||||
|
||||
uint16_t mf_df_prepare_get_version(uint8_t* dest) {
|
||||
dest[0] = MF_DF_GET_VERSION;
|
||||
return 1;
|
||||
}
|
||||
|
||||
bool mf_df_parse_get_version_response(uint8_t* buf, uint16_t len, MifareDesfireVersion* out) {
|
||||
if(len < 1 || *buf) {
|
||||
return false;
|
||||
}
|
||||
len--;
|
||||
buf++;
|
||||
if(len < sizeof(MifareDesfireVersion)) {
|
||||
return false;
|
||||
}
|
||||
memcpy(out, buf, sizeof(MifareDesfireVersion));
|
||||
return true;
|
||||
}
|
||||
|
||||
uint16_t mf_df_prepare_get_free_memory(uint8_t* dest) {
|
||||
dest[0] = MF_DF_GET_FREE_MEMORY;
|
||||
return 1;
|
||||
}
|
||||
|
||||
bool mf_df_parse_get_free_memory_response(uint8_t* buf, uint16_t len, MifareDesfireFreeMemory* out) {
|
||||
if(len < 1 || *buf) {
|
||||
return false;
|
||||
}
|
||||
len--;
|
||||
buf++;
|
||||
if(len != 3) {
|
||||
return false;
|
||||
}
|
||||
out->bytes = buf[0] | (buf[1] << 8) | (buf[2] << 16);
|
||||
return true;
|
||||
}
|
||||
|
||||
uint16_t mf_df_prepare_get_key_settings(uint8_t* dest) {
|
||||
dest[0] = MF_DF_GET_KEY_SETTINGS;
|
||||
return 1;
|
||||
}
|
||||
|
||||
bool mf_df_parse_get_key_settings_response(
|
||||
uint8_t* buf,
|
||||
uint16_t len,
|
||||
MifareDesfireKeySettings* out) {
|
||||
if(len < 1 || *buf) {
|
||||
return false;
|
||||
}
|
||||
len--;
|
||||
buf++;
|
||||
if(len < 2) {
|
||||
return false;
|
||||
}
|
||||
out->change_key_id = buf[0] >> 4;
|
||||
out->config_changeable = (buf[0] & 0x8) != 0;
|
||||
out->free_create_delete = (buf[0] & 0x4) != 0;
|
||||
out->free_directory_list = (buf[0] & 0x2) != 0;
|
||||
out->master_key_changeable = (buf[0] & 0x1) != 0;
|
||||
out->flags = buf[1] >> 4;
|
||||
out->max_keys = buf[1] & 0xF;
|
||||
return true;
|
||||
}
|
||||
|
||||
uint16_t mf_df_prepare_get_key_version(uint8_t* dest, uint8_t key_id) {
|
||||
dest[0] = MF_DF_GET_KEY_VERSION;
|
||||
dest[1] = key_id;
|
||||
return 2;
|
||||
}
|
||||
|
||||
bool mf_df_parse_get_key_version_response(uint8_t* buf, uint16_t len, MifareDesfireKeyVersion* out) {
|
||||
if(len != 2 || *buf) {
|
||||
return false;
|
||||
}
|
||||
out->version = buf[1];
|
||||
return true;
|
||||
}
|
||||
|
||||
uint16_t mf_df_prepare_get_application_ids(uint8_t* dest) {
|
||||
dest[0] = MF_DF_GET_APPLICATION_IDS;
|
||||
return 1;
|
||||
}
|
||||
|
||||
bool mf_df_parse_get_application_ids_response(
|
||||
uint8_t* buf,
|
||||
uint16_t len,
|
||||
MifareDesfireApplication** app_head) {
|
||||
if(len < 1 || *buf) {
|
||||
return false;
|
||||
}
|
||||
len--;
|
||||
buf++;
|
||||
if(len % 3 != 0) {
|
||||
return false;
|
||||
}
|
||||
while(len) {
|
||||
MifareDesfireApplication* app = malloc(sizeof(MifareDesfireApplication));
|
||||
memset(app, 0, sizeof(MifareDesfireApplication));
|
||||
memcpy(app->id, buf, 3);
|
||||
len -= 3;
|
||||
buf += 3;
|
||||
*app_head = app;
|
||||
app_head = &app->next;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
uint16_t mf_df_prepare_select_application(uint8_t* dest, uint8_t id[3]) {
|
||||
dest[0] = MF_DF_SELECT_APPLICATION;
|
||||
dest[1] = id[0];
|
||||
dest[2] = id[1];
|
||||
dest[3] = id[2];
|
||||
return 4;
|
||||
}
|
||||
|
||||
bool mf_df_parse_select_application_response(uint8_t* buf, uint16_t len) {
|
||||
return len == 1 && !*buf;
|
||||
}
|
||||
|
||||
uint16_t mf_df_prepare_get_file_ids(uint8_t* dest) {
|
||||
dest[0] = MF_DF_GET_FILE_IDS;
|
||||
return 1;
|
||||
}
|
||||
|
||||
bool mf_df_parse_get_file_ids_response(uint8_t* buf, uint16_t len, MifareDesfireFile** file_head) {
|
||||
if(len < 1 || *buf) {
|
||||
return false;
|
||||
}
|
||||
len--;
|
||||
buf++;
|
||||
while(len) {
|
||||
MifareDesfireFile* file = malloc(sizeof(MifareDesfireFile));
|
||||
memset(file, 0, sizeof(MifareDesfireFile));
|
||||
file->id = *buf;
|
||||
len--;
|
||||
buf++;
|
||||
*file_head = file;
|
||||
file_head = &file->next;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
uint16_t mf_df_prepare_get_file_settings(uint8_t* dest, uint8_t file_id) {
|
||||
dest[0] = MF_DF_GET_FILE_SETTINGS;
|
||||
dest[1] = file_id;
|
||||
return 2;
|
||||
}
|
||||
|
||||
bool mf_df_parse_get_file_settings_response(uint8_t* buf, uint16_t len, MifareDesfireFile* out) {
|
||||
if(len < 5 || *buf) {
|
||||
return false;
|
||||
}
|
||||
len--;
|
||||
buf++;
|
||||
out->type = buf[0];
|
||||
out->comm = buf[1];
|
||||
out->access_rights = buf[2] | (buf[3] << 8);
|
||||
switch(out->type) {
|
||||
case MifareDesfireFileTypeStandard:
|
||||
case MifareDesfireFileTypeBackup:
|
||||
if(len != 7) {
|
||||
return false;
|
||||
}
|
||||
out->settings.data.size = buf[4] | (buf[5] << 8) | (buf[6] << 16);
|
||||
break;
|
||||
case MifareDesfireFileTypeValue:
|
||||
if(len != 17) {
|
||||
return false;
|
||||
}
|
||||
out->settings.value.lo_limit = buf[4] | (buf[5] << 8) | (buf[6] << 16) | (buf[7] << 24);
|
||||
out->settings.value.hi_limit = buf[8] | (buf[9] << 8) | (buf[10] << 16) | (buf[11] << 24);
|
||||
out->settings.value.limited_credit_value = buf[12] | (buf[13] << 8) | (buf[14] << 16) |
|
||||
(buf[15] << 24);
|
||||
out->settings.value.limited_credit_enabled = buf[16];
|
||||
break;
|
||||
case MifareDesfireFileTypeLinearRecord:
|
||||
case MifareDesfireFileTypeCyclicRecord:
|
||||
if(len != 13) {
|
||||
return false;
|
||||
}
|
||||
out->settings.record.size = buf[4] | (buf[5] << 8) | (buf[6] << 16);
|
||||
out->settings.record.max = buf[7] | (buf[8] << 8) | (buf[9] << 16);
|
||||
out->settings.record.cur = buf[10] | (buf[11] << 8) | (buf[12] << 16);
|
||||
break;
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
uint16_t mf_df_prepare_read_data(uint8_t* dest, uint8_t file_id, uint32_t offset, uint32_t len) {
|
||||
dest[0] = MF_DF_READ_DATA;
|
||||
dest[1] = file_id;
|
||||
dest[2] = offset;
|
||||
dest[3] = offset >> 8;
|
||||
dest[4] = offset >> 16;
|
||||
dest[5] = len;
|
||||
dest[6] = len >> 8;
|
||||
dest[7] = len >> 16;
|
||||
return 8;
|
||||
}
|
||||
|
||||
uint16_t mf_df_prepare_get_value(uint8_t* dest, uint8_t file_id) {
|
||||
dest[0] = MF_DF_GET_VALUE;
|
||||
dest[1] = file_id;
|
||||
return 2;
|
||||
}
|
||||
|
||||
uint16_t
|
||||
mf_df_prepare_read_records(uint8_t* dest, uint8_t file_id, uint32_t offset, uint32_t len) {
|
||||
dest[0] = MF_DF_READ_RECORDS;
|
||||
dest[1] = file_id;
|
||||
dest[2] = offset;
|
||||
dest[3] = offset >> 8;
|
||||
dest[4] = offset >> 16;
|
||||
dest[5] = len;
|
||||
dest[6] = len >> 8;
|
||||
dest[7] = len >> 16;
|
||||
return 8;
|
||||
}
|
||||
|
||||
bool mf_df_parse_read_data_response(uint8_t* buf, uint16_t len, MifareDesfireFile* out) {
|
||||
if(len < 1 || *buf) {
|
||||
return false;
|
||||
}
|
||||
len--;
|
||||
buf++;
|
||||
out->contents = malloc(len);
|
||||
memcpy(out->contents, buf, len);
|
||||
return true;
|
||||
}
|
||||
|
||||
bool mf_df_read_card(FuriHalNfcTxRxContext* tx_rx, MifareDesfireData* data) {
|
||||
furi_assert(tx_rx);
|
||||
furi_assert(data);
|
||||
|
||||
bool card_read = false;
|
||||
do {
|
||||
// Get version
|
||||
tx_rx->tx_bits = 8 * mf_df_prepare_get_version(tx_rx->tx_data);
|
||||
if(!furi_hal_nfc_tx_rx_full(tx_rx)) {
|
||||
FURI_LOG_W(TAG, "Bad exchange getting version");
|
||||
break;
|
||||
}
|
||||
if(!mf_df_parse_get_version_response(tx_rx->rx_data, tx_rx->rx_bits / 8, &data->version)) {
|
||||
FURI_LOG_W(TAG, "Bad DESFire GET_VERSION responce");
|
||||
}
|
||||
|
||||
// Get free memory
|
||||
tx_rx->tx_bits = 8 * mf_df_prepare_get_free_memory(tx_rx->tx_data);
|
||||
if(furi_hal_nfc_tx_rx_full(tx_rx)) {
|
||||
data->free_memory = malloc(sizeof(MifareDesfireFreeMemory));
|
||||
if(!mf_df_parse_get_free_memory_response(
|
||||
tx_rx->rx_data, tx_rx->rx_bits / 8, data->free_memory)) {
|
||||
FURI_LOG_D(TAG, "Bad DESFire GET_FREE_MEMORY response (normal for pre-EV1 cards)");
|
||||
free(data->free_memory);
|
||||
data->free_memory = NULL;
|
||||
}
|
||||
}
|
||||
|
||||
// Get key settings
|
||||
tx_rx->tx_bits = 8 * mf_df_prepare_get_key_settings(tx_rx->tx_data);
|
||||
if(!furi_hal_nfc_tx_rx_full(tx_rx)) {
|
||||
FURI_LOG_D(TAG, "Bad exchange getting key settings");
|
||||
} else {
|
||||
data->master_key_settings = malloc(sizeof(MifareDesfireKeySettings));
|
||||
if(!mf_df_parse_get_key_settings_response(
|
||||
tx_rx->rx_data, tx_rx->rx_bits / 8, data->master_key_settings)) {
|
||||
FURI_LOG_W(TAG, "Bad DESFire GET_KEY_SETTINGS response");
|
||||
free(data->master_key_settings);
|
||||
data->master_key_settings = NULL;
|
||||
} else {
|
||||
MifareDesfireKeyVersion** key_version_head =
|
||||
&data->master_key_settings->key_version_head;
|
||||
for(uint8_t key_id = 0; key_id < data->master_key_settings->max_keys; key_id++) {
|
||||
tx_rx->tx_bits = 8 * mf_df_prepare_get_key_version(tx_rx->tx_data, key_id);
|
||||
if(!furi_hal_nfc_tx_rx_full(tx_rx)) {
|
||||
FURI_LOG_W(TAG, "Bad exchange getting key version");
|
||||
continue;
|
||||
}
|
||||
MifareDesfireKeyVersion* key_version = malloc(sizeof(MifareDesfireKeyVersion));
|
||||
memset(key_version, 0, sizeof(MifareDesfireKeyVersion));
|
||||
key_version->id = key_id;
|
||||
if(!mf_df_parse_get_key_version_response(
|
||||
tx_rx->rx_data, tx_rx->rx_bits / 8, key_version)) {
|
||||
FURI_LOG_W(TAG, "Bad DESFire GET_KEY_VERSION response");
|
||||
free(key_version);
|
||||
continue;
|
||||
}
|
||||
*key_version_head = key_version;
|
||||
key_version_head = &key_version->next;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Get application IDs
|
||||
tx_rx->tx_bits = 8 * mf_df_prepare_get_application_ids(tx_rx->tx_data);
|
||||
if(!furi_hal_nfc_tx_rx_full(tx_rx)) {
|
||||
FURI_LOG_W(TAG, "Bad exchange getting application IDs");
|
||||
break;
|
||||
} else {
|
||||
if(!mf_df_parse_get_application_ids_response(
|
||||
tx_rx->rx_data, tx_rx->rx_bits / 8, &data->app_head)) {
|
||||
FURI_LOG_W(TAG, "Bad DESFire GET_APPLICATION_IDS response");
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
for(MifareDesfireApplication* app = data->app_head; app; app = app->next) {
|
||||
tx_rx->tx_bits = 8 * mf_df_prepare_select_application(tx_rx->tx_data, app->id);
|
||||
if(!furi_hal_nfc_tx_rx_full(tx_rx) ||
|
||||
!mf_df_parse_select_application_response(tx_rx->rx_data, tx_rx->rx_bits / 8)) {
|
||||
FURI_LOG_W(TAG, "Bad exchange selecting application");
|
||||
continue;
|
||||
}
|
||||
tx_rx->tx_bits = 8 * mf_df_prepare_get_key_settings(tx_rx->tx_data);
|
||||
if(!furi_hal_nfc_tx_rx_full(tx_rx)) {
|
||||
FURI_LOG_W(TAG, "Bad exchange getting key settings");
|
||||
} else {
|
||||
app->key_settings = malloc(sizeof(MifareDesfireKeySettings));
|
||||
memset(app->key_settings, 0, sizeof(MifareDesfireKeySettings));
|
||||
if(!mf_df_parse_get_key_settings_response(
|
||||
tx_rx->rx_data, tx_rx->rx_bits / 8, app->key_settings)) {
|
||||
FURI_LOG_W(TAG, "Bad DESFire GET_KEY_SETTINGS response");
|
||||
free(app->key_settings);
|
||||
app->key_settings = NULL;
|
||||
continue;
|
||||
}
|
||||
|
||||
MifareDesfireKeyVersion** key_version_head = &app->key_settings->key_version_head;
|
||||
for(uint8_t key_id = 0; key_id < app->key_settings->max_keys; key_id++) {
|
||||
tx_rx->tx_bits = 8 * mf_df_prepare_get_key_version(tx_rx->tx_data, key_id);
|
||||
if(!furi_hal_nfc_tx_rx_full(tx_rx)) {
|
||||
FURI_LOG_W(TAG, "Bad exchange getting key version");
|
||||
continue;
|
||||
}
|
||||
MifareDesfireKeyVersion* key_version = malloc(sizeof(MifareDesfireKeyVersion));
|
||||
memset(key_version, 0, sizeof(MifareDesfireKeyVersion));
|
||||
key_version->id = key_id;
|
||||
if(!mf_df_parse_get_key_version_response(
|
||||
tx_rx->rx_data, tx_rx->rx_bits / 8, key_version)) {
|
||||
FURI_LOG_W(TAG, "Bad DESFire GET_KEY_VERSION response");
|
||||
free(key_version);
|
||||
continue;
|
||||
}
|
||||
*key_version_head = key_version;
|
||||
key_version_head = &key_version->next;
|
||||
}
|
||||
}
|
||||
|
||||
tx_rx->tx_bits = 8 * mf_df_prepare_get_file_ids(tx_rx->tx_data);
|
||||
if(!furi_hal_nfc_tx_rx_full(tx_rx)) {
|
||||
FURI_LOG_W(TAG, "Bad exchange getting file IDs");
|
||||
} else {
|
||||
if(!mf_df_parse_get_file_ids_response(
|
||||
tx_rx->rx_data, tx_rx->rx_bits / 8, &app->file_head)) {
|
||||
FURI_LOG_W(TAG, "Bad DESFire GET_FILE_IDS response");
|
||||
}
|
||||
}
|
||||
|
||||
for(MifareDesfireFile* file = app->file_head; file; file = file->next) {
|
||||
tx_rx->tx_bits = 8 * mf_df_prepare_get_file_settings(tx_rx->tx_data, file->id);
|
||||
if(!furi_hal_nfc_tx_rx_full(tx_rx)) {
|
||||
FURI_LOG_W(TAG, "Bad exchange getting file settings");
|
||||
continue;
|
||||
}
|
||||
if(!mf_df_parse_get_file_settings_response(
|
||||
tx_rx->rx_data, tx_rx->rx_bits / 8, file)) {
|
||||
FURI_LOG_W(TAG, "Bad DESFire GET_FILE_SETTINGS response");
|
||||
continue;
|
||||
}
|
||||
switch(file->type) {
|
||||
case MifareDesfireFileTypeStandard:
|
||||
case MifareDesfireFileTypeBackup:
|
||||
tx_rx->tx_bits = 8 * mf_df_prepare_read_data(tx_rx->tx_data, file->id, 0, 0);
|
||||
break;
|
||||
case MifareDesfireFileTypeValue:
|
||||
tx_rx->tx_bits = 8 * mf_df_prepare_get_value(tx_rx->tx_data, file->id);
|
||||
break;
|
||||
case MifareDesfireFileTypeLinearRecord:
|
||||
case MifareDesfireFileTypeCyclicRecord:
|
||||
tx_rx->tx_bits =
|
||||
8 * mf_df_prepare_read_records(tx_rx->tx_data, file->id, 0, 0);
|
||||
break;
|
||||
}
|
||||
if(!furi_hal_nfc_tx_rx_full(tx_rx)) {
|
||||
FURI_LOG_W(TAG, "Bad exchange reading file %d", file->id);
|
||||
continue;
|
||||
}
|
||||
if(!mf_df_parse_read_data_response(tx_rx->rx_data, tx_rx->rx_bits / 8, file)) {
|
||||
FURI_LOG_W(TAG, "Bad response reading file %d", file->id);
|
||||
continue;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
card_read = true;
|
||||
} while(false);
|
||||
|
||||
return card_read;
|
||||
}
|
||||
169
lib/nfc/protocols/mifare_desfire.h
Normal file
169
lib/nfc/protocols/mifare_desfire.h
Normal file
@@ -0,0 +1,169 @@
|
||||
#pragma once
|
||||
|
||||
#include <m-string.h>
|
||||
#include <stdint.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
#include <furi_hal_nfc.h>
|
||||
|
||||
#define MF_DF_GET_VERSION (0x60)
|
||||
#define MF_DF_GET_FREE_MEMORY (0x6E)
|
||||
#define MF_DF_GET_KEY_SETTINGS (0x45)
|
||||
#define MF_DF_GET_KEY_VERSION (0x64)
|
||||
#define MF_DF_GET_APPLICATION_IDS (0x6A)
|
||||
#define MF_DF_SELECT_APPLICATION (0x5A)
|
||||
#define MF_DF_GET_FILE_IDS (0x6F)
|
||||
#define MF_DF_GET_FILE_SETTINGS (0xF5)
|
||||
|
||||
#define MF_DF_READ_DATA (0xBD)
|
||||
#define MF_DF_GET_VALUE (0x6C)
|
||||
#define MF_DF_READ_RECORDS (0xBB)
|
||||
|
||||
typedef struct {
|
||||
uint8_t hw_vendor;
|
||||
uint8_t hw_type;
|
||||
uint8_t hw_subtype;
|
||||
uint8_t hw_major;
|
||||
uint8_t hw_minor;
|
||||
uint8_t hw_storage;
|
||||
uint8_t hw_proto;
|
||||
|
||||
uint8_t sw_vendor;
|
||||
uint8_t sw_type;
|
||||
uint8_t sw_subtype;
|
||||
uint8_t sw_major;
|
||||
uint8_t sw_minor;
|
||||
uint8_t sw_storage;
|
||||
uint8_t sw_proto;
|
||||
|
||||
uint8_t uid[7];
|
||||
uint8_t batch[5];
|
||||
uint8_t prod_week;
|
||||
uint8_t prod_year;
|
||||
} MifareDesfireVersion;
|
||||
|
||||
typedef struct {
|
||||
uint32_t bytes;
|
||||
} MifareDesfireFreeMemory; // EV1+ only
|
||||
|
||||
typedef struct MifareDesfireKeyVersion {
|
||||
uint8_t id;
|
||||
uint8_t version;
|
||||
struct MifareDesfireKeyVersion* next;
|
||||
} MifareDesfireKeyVersion;
|
||||
|
||||
typedef struct {
|
||||
uint8_t change_key_id;
|
||||
bool config_changeable;
|
||||
bool free_create_delete;
|
||||
bool free_directory_list;
|
||||
bool master_key_changeable;
|
||||
uint8_t flags;
|
||||
uint8_t max_keys;
|
||||
MifareDesfireKeyVersion* key_version_head;
|
||||
} MifareDesfireKeySettings;
|
||||
|
||||
typedef enum {
|
||||
MifareDesfireFileTypeStandard = 0,
|
||||
MifareDesfireFileTypeBackup = 1,
|
||||
MifareDesfireFileTypeValue = 2,
|
||||
MifareDesfireFileTypeLinearRecord = 3,
|
||||
MifareDesfireFileTypeCyclicRecord = 4,
|
||||
} MifareDesfireFileType;
|
||||
|
||||
typedef enum {
|
||||
MifareDesfireFileCommunicationSettingsPlaintext = 0,
|
||||
MifareDesfireFileCommunicationSettingsAuthenticated = 1,
|
||||
MifareDesfireFileCommunicationSettingsEnciphered = 3,
|
||||
} MifareDesfireFileCommunicationSettings;
|
||||
|
||||
typedef struct MifareDesfireFile {
|
||||
uint8_t id;
|
||||
MifareDesfireFileType type;
|
||||
MifareDesfireFileCommunicationSettings comm;
|
||||
uint16_t access_rights;
|
||||
union {
|
||||
struct {
|
||||
uint32_t size;
|
||||
} data;
|
||||
struct {
|
||||
uint32_t lo_limit;
|
||||
uint32_t hi_limit;
|
||||
uint32_t limited_credit_value;
|
||||
bool limited_credit_enabled;
|
||||
} value;
|
||||
struct {
|
||||
uint32_t size;
|
||||
uint32_t max;
|
||||
uint32_t cur;
|
||||
} record;
|
||||
} settings;
|
||||
uint8_t* contents;
|
||||
|
||||
struct MifareDesfireFile* next;
|
||||
} MifareDesfireFile;
|
||||
|
||||
typedef struct MifareDesfireApplication {
|
||||
uint8_t id[3];
|
||||
MifareDesfireKeySettings* key_settings;
|
||||
MifareDesfireFile* file_head;
|
||||
|
||||
struct MifareDesfireApplication* next;
|
||||
} MifareDesfireApplication;
|
||||
|
||||
typedef struct {
|
||||
MifareDesfireVersion version;
|
||||
MifareDesfireFreeMemory* free_memory;
|
||||
MifareDesfireKeySettings* master_key_settings;
|
||||
MifareDesfireApplication* app_head;
|
||||
} MifareDesfireData;
|
||||
|
||||
void mf_df_clear(MifareDesfireData* data);
|
||||
|
||||
void mf_df_cat_data(MifareDesfireData* data, string_t out);
|
||||
void mf_df_cat_card_info(MifareDesfireData* data, string_t out);
|
||||
void mf_df_cat_version(MifareDesfireVersion* version, string_t out);
|
||||
void mf_df_cat_free_mem(MifareDesfireFreeMemory* free_mem, string_t out);
|
||||
void mf_df_cat_key_settings(MifareDesfireKeySettings* ks, string_t out);
|
||||
void mf_df_cat_application_info(MifareDesfireApplication* app, string_t out);
|
||||
void mf_df_cat_application(MifareDesfireApplication* app, string_t out);
|
||||
void mf_df_cat_file(MifareDesfireFile* file, string_t out);
|
||||
|
||||
bool mf_df_check_card_type(uint8_t ATQA0, uint8_t ATQA1, uint8_t SAK);
|
||||
|
||||
uint16_t mf_df_prepare_get_version(uint8_t* dest);
|
||||
bool mf_df_parse_get_version_response(uint8_t* buf, uint16_t len, MifareDesfireVersion* out);
|
||||
|
||||
uint16_t mf_df_prepare_get_free_memory(uint8_t* dest);
|
||||
bool mf_df_parse_get_free_memory_response(uint8_t* buf, uint16_t len, MifareDesfireFreeMemory* out);
|
||||
|
||||
uint16_t mf_df_prepare_get_key_settings(uint8_t* dest);
|
||||
bool mf_df_parse_get_key_settings_response(
|
||||
uint8_t* buf,
|
||||
uint16_t len,
|
||||
MifareDesfireKeySettings* out);
|
||||
|
||||
uint16_t mf_df_prepare_get_key_version(uint8_t* dest, uint8_t key_id);
|
||||
bool mf_df_parse_get_key_version_response(uint8_t* buf, uint16_t len, MifareDesfireKeyVersion* out);
|
||||
|
||||
uint16_t mf_df_prepare_get_application_ids(uint8_t* dest);
|
||||
bool mf_df_parse_get_application_ids_response(
|
||||
uint8_t* buf,
|
||||
uint16_t len,
|
||||
MifareDesfireApplication** app_head);
|
||||
|
||||
uint16_t mf_df_prepare_select_application(uint8_t* dest, uint8_t id[3]);
|
||||
bool mf_df_parse_select_application_response(uint8_t* buf, uint16_t len);
|
||||
|
||||
uint16_t mf_df_prepare_get_file_ids(uint8_t* dest);
|
||||
bool mf_df_parse_get_file_ids_response(uint8_t* buf, uint16_t len, MifareDesfireFile** file_head);
|
||||
|
||||
uint16_t mf_df_prepare_get_file_settings(uint8_t* dest, uint8_t file_id);
|
||||
bool mf_df_parse_get_file_settings_response(uint8_t* buf, uint16_t len, MifareDesfireFile* out);
|
||||
|
||||
uint16_t mf_df_prepare_read_data(uint8_t* dest, uint8_t file_id, uint32_t offset, uint32_t len);
|
||||
uint16_t mf_df_prepare_get_value(uint8_t* dest, uint8_t file_id);
|
||||
uint16_t mf_df_prepare_read_records(uint8_t* dest, uint8_t file_id, uint32_t offset, uint32_t len);
|
||||
bool mf_df_parse_read_data_response(uint8_t* buf, uint16_t len, MifareDesfireFile* out);
|
||||
|
||||
bool mf_df_read_card(FuriHalNfcTxRxContext* tx_rx, MifareDesfireData* data);
|
||||
1791
lib/nfc/protocols/mifare_ultralight.c
Normal file
1791
lib/nfc/protocols/mifare_ultralight.c
Normal file
File diff suppressed because it is too large
Load Diff
222
lib/nfc/protocols/mifare_ultralight.h
Normal file
222
lib/nfc/protocols/mifare_ultralight.h
Normal file
@@ -0,0 +1,222 @@
|
||||
#pragma once
|
||||
|
||||
#include <furi_hal_nfc.h>
|
||||
|
||||
// Largest tag is NTAG I2C Plus 2K, both data sectors plus SRAM
|
||||
#define MF_UL_MAX_DUMP_SIZE ((238 + 256 + 16) * 4)
|
||||
|
||||
#define MF_UL_TEARING_FLAG_DEFAULT (0xBD)
|
||||
|
||||
#define MF_UL_HALT_START (0x50)
|
||||
#define MF_UL_GET_VERSION_CMD (0x60)
|
||||
#define MF_UL_READ_CMD (0x30)
|
||||
#define MF_UL_FAST_READ_CMD (0x3A)
|
||||
#define MF_UL_WRITE (0xA2)
|
||||
#define MF_UL_FAST_WRITE (0xA6)
|
||||
#define MF_UL_COMP_WRITE (0xA0)
|
||||
#define MF_UL_READ_CNT (0x39)
|
||||
#define MF_UL_INC_CNT (0xA5)
|
||||
#define MF_UL_AUTH (0x1B)
|
||||
#define MF_UL_READ_SIG (0x3C)
|
||||
#define MF_UL_CHECK_TEARING (0x3E)
|
||||
#define MF_UL_READ_VCSL (0x4B)
|
||||
#define MF_UL_SECTOR_SELECT (0xC2)
|
||||
|
||||
#define MF_UL_ACK (0xa)
|
||||
#define MF_UL_NAK_INVALID_ARGUMENT (0x0)
|
||||
#define MF_UL_NAK_AUTHLIM_REACHED (0x4)
|
||||
|
||||
#define MF_UL_NTAG203_COUNTER_PAGE (41)
|
||||
|
||||
// Important: order matters; some features are based on positioning in this enum
|
||||
typedef enum {
|
||||
MfUltralightTypeUnknown,
|
||||
MfUltralightTypeNTAG203,
|
||||
// Below have config pages and GET_VERSION support
|
||||
MfUltralightTypeUL11,
|
||||
MfUltralightTypeUL21,
|
||||
MfUltralightTypeNTAG213,
|
||||
MfUltralightTypeNTAG215,
|
||||
MfUltralightTypeNTAG216,
|
||||
// Below also have sector select
|
||||
// NTAG I2C's *does not* have regular config pages, so it's a bit of an odd duck
|
||||
MfUltralightTypeNTAGI2C1K,
|
||||
MfUltralightTypeNTAGI2C2K,
|
||||
// NTAG I2C Plus has stucture expected from NTAG21x
|
||||
MfUltralightTypeNTAGI2CPlus1K,
|
||||
MfUltralightTypeNTAGI2CPlus2K,
|
||||
|
||||
// Keep last for number of types calculation
|
||||
MfUltralightTypeNum,
|
||||
} MfUltralightType;
|
||||
|
||||
typedef enum {
|
||||
MfUltralightSupportNone = 0,
|
||||
MfUltralightSupportFastRead = 1 << 0,
|
||||
MfUltralightSupportTearingFlags = 1 << 1,
|
||||
MfUltralightSupportReadCounter = 1 << 2,
|
||||
MfUltralightSupportIncrCounter = 1 << 3,
|
||||
MfUltralightSupportSignature = 1 << 4,
|
||||
MfUltralightSupportFastWrite = 1 << 5,
|
||||
MfUltralightSupportCompatWrite = 1 << 6,
|
||||
MfUltralightSupportAuth = 1 << 7,
|
||||
MfUltralightSupportVcsl = 1 << 8,
|
||||
MfUltralightSupportSectorSelect = 1 << 9,
|
||||
// NTAG21x only has counter 2
|
||||
MfUltralightSupportSingleCounter = 1 << 10,
|
||||
// ASCII mirror is not a command, but handy to have as a flag
|
||||
MfUltralightSupportAsciiMirror = 1 << 11,
|
||||
// NTAG203 counter that's in memory rather than through a command
|
||||
MfUltralightSupportCounterInMemory = 1 << 12,
|
||||
} MfUltralightFeatures;
|
||||
|
||||
typedef enum {
|
||||
MfUltralightMirrorNone,
|
||||
MfUltralightMirrorUid,
|
||||
MfUltralightMirrorCounter,
|
||||
MfUltralightMirrorUidCounter,
|
||||
} MfUltralightMirrorConf;
|
||||
|
||||
typedef struct {
|
||||
uint8_t header;
|
||||
uint8_t vendor_id;
|
||||
uint8_t prod_type;
|
||||
uint8_t prod_subtype;
|
||||
uint8_t prod_ver_major;
|
||||
uint8_t prod_ver_minor;
|
||||
uint8_t storage_size;
|
||||
uint8_t protocol_type;
|
||||
} MfUltralightVersion;
|
||||
|
||||
typedef struct {
|
||||
uint8_t sn0[3];
|
||||
uint8_t btBCC0;
|
||||
uint8_t sn1[4];
|
||||
uint8_t btBCC1;
|
||||
uint8_t internal;
|
||||
uint8_t lock[2];
|
||||
uint8_t otp[4];
|
||||
} MfUltralightManufacturerBlock;
|
||||
|
||||
typedef struct {
|
||||
MfUltralightType type;
|
||||
MfUltralightVersion version;
|
||||
uint8_t signature[32];
|
||||
uint32_t counter[3];
|
||||
uint8_t tearing[3];
|
||||
uint16_t curr_authlim;
|
||||
uint16_t data_size;
|
||||
uint8_t data[MF_UL_MAX_DUMP_SIZE];
|
||||
} MfUltralightData;
|
||||
|
||||
typedef struct __attribute__((packed)) {
|
||||
union {
|
||||
uint8_t raw[4];
|
||||
uint32_t value;
|
||||
} pwd;
|
||||
union {
|
||||
uint8_t raw[2];
|
||||
uint16_t value;
|
||||
} pack;
|
||||
} MfUltralightAuth;
|
||||
|
||||
// Common configuration pages for MFUL EV1, NTAG21x, and NTAG I2C Plus
|
||||
typedef struct __attribute__((packed)) {
|
||||
union {
|
||||
uint8_t value;
|
||||
struct {
|
||||
uint8_t rfui1 : 2;
|
||||
bool strg_mod_en : 1;
|
||||
bool rfui2 : 1;
|
||||
uint8_t mirror_byte : 2;
|
||||
MfUltralightMirrorConf mirror_conf : 2;
|
||||
};
|
||||
} mirror;
|
||||
uint8_t rfui1;
|
||||
uint8_t mirror_page;
|
||||
uint8_t auth0;
|
||||
union {
|
||||
uint8_t value;
|
||||
struct {
|
||||
uint8_t authlim : 3;
|
||||
bool nfc_cnt_pwd_prot : 1;
|
||||
bool nfc_cnt_en : 1;
|
||||
bool nfc_dis_sec1 : 1; // NTAG I2C Plus only
|
||||
bool cfglck : 1;
|
||||
bool prot : 1;
|
||||
};
|
||||
} access;
|
||||
uint8_t vctid;
|
||||
uint8_t rfui2[2];
|
||||
MfUltralightAuth auth_data;
|
||||
uint8_t rfui3[2];
|
||||
} MfUltralightConfigPages;
|
||||
|
||||
typedef struct {
|
||||
uint16_t pages_to_read;
|
||||
int16_t pages_read;
|
||||
MfUltralightFeatures supported_features;
|
||||
} MfUltralightReader;
|
||||
|
||||
typedef struct {
|
||||
MfUltralightData data;
|
||||
MfUltralightConfigPages* config;
|
||||
// Most config values don't apply until power cycle, so cache config pages
|
||||
// for correct behavior
|
||||
MfUltralightConfigPages config_cache;
|
||||
MfUltralightFeatures supported_features;
|
||||
uint16_t page_num;
|
||||
bool data_changed;
|
||||
bool comp_write_cmd_started;
|
||||
uint8_t comp_write_page_addr;
|
||||
bool auth_success;
|
||||
uint8_t curr_sector;
|
||||
bool sector_select_cmd_started;
|
||||
bool ntag_i2c_plus_sector3_lockout;
|
||||
bool read_counter_incremented;
|
||||
} MfUltralightEmulator;
|
||||
|
||||
bool mf_ul_check_card_type(uint8_t ATQA0, uint8_t ATQA1, uint8_t SAK);
|
||||
|
||||
bool mf_ultralight_read_version(
|
||||
FuriHalNfcTxRxContext* tx_rx,
|
||||
MfUltralightReader* reader,
|
||||
MfUltralightData* data);
|
||||
|
||||
bool mf_ultralight_read_pages_direct(
|
||||
FuriHalNfcTxRxContext* tx_rx,
|
||||
uint8_t start_index,
|
||||
uint8_t* data);
|
||||
|
||||
bool mf_ultralight_read_pages(
|
||||
FuriHalNfcTxRxContext* tx_rx,
|
||||
MfUltralightReader* reader,
|
||||
MfUltralightData* data);
|
||||
|
||||
bool mf_ultralight_fast_read_pages(
|
||||
FuriHalNfcTxRxContext* tx_rx,
|
||||
MfUltralightReader* reader,
|
||||
MfUltralightData* data);
|
||||
|
||||
bool mf_ultralight_read_signature(FuriHalNfcTxRxContext* tx_rx, MfUltralightData* data);
|
||||
|
||||
bool mf_ultralight_read_counters(FuriHalNfcTxRxContext* tx_rx, MfUltralightData* data);
|
||||
|
||||
bool mf_ultralight_read_tearing_flags(FuriHalNfcTxRxContext* tx_rx, MfUltralightData* data);
|
||||
|
||||
bool mf_ul_read_card(
|
||||
FuriHalNfcTxRxContext* tx_rx,
|
||||
MfUltralightReader* reader,
|
||||
MfUltralightData* data);
|
||||
|
||||
void mf_ul_reset_emulation(MfUltralightEmulator* emulator, bool is_power_cycle);
|
||||
|
||||
void mf_ul_prepare_emulation(MfUltralightEmulator* emulator, MfUltralightData* data);
|
||||
|
||||
bool mf_ul_prepare_emulation_response(
|
||||
uint8_t* buff_rx,
|
||||
uint16_t buff_rx_len,
|
||||
uint8_t* buff_tx,
|
||||
uint16_t* buff_tx_len,
|
||||
uint32_t* data_type,
|
||||
void* context);
|
||||
47
lib/nfc/protocols/nfc_util.c
Normal file
47
lib/nfc/protocols/nfc_util.c
Normal file
@@ -0,0 +1,47 @@
|
||||
#include "nfc_util.h"
|
||||
|
||||
#include <furi.h>
|
||||
|
||||
static const uint8_t nfc_util_odd_byte_parity[256] = {
|
||||
1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1, 0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0,
|
||||
1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0, 1, 0, 0, 1, 0, 1, 1, 0, 0, 1,
|
||||
1, 0, 1, 0, 0, 1, 0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0, 1, 0, 0, 1, 0, 1, 1,
|
||||
0, 0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1, 0, 1, 1, 0,
|
||||
1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0, 1,
|
||||
0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0,
|
||||
0, 1, 0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0, 1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1,
|
||||
0, 1, 0, 0, 1, 0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1,
|
||||
1, 0, 0, 1, 0, 1, 1, 0, 1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1};
|
||||
|
||||
void nfc_util_num2bytes(uint64_t src, uint8_t len, uint8_t* dest) {
|
||||
furi_assert(dest);
|
||||
furi_assert(len <= 8);
|
||||
|
||||
while(len--) {
|
||||
dest[len] = (uint8_t)src;
|
||||
src >>= 8;
|
||||
}
|
||||
}
|
||||
|
||||
uint64_t nfc_util_bytes2num(uint8_t* src, uint8_t len) {
|
||||
furi_assert(src);
|
||||
furi_assert(len <= 8);
|
||||
|
||||
uint64_t res = 0;
|
||||
while(len--) {
|
||||
res = (res << 8) | (*src);
|
||||
src++;
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
uint8_t nfc_util_even_parity32(uint32_t data) {
|
||||
// data ^= data >> 16;
|
||||
// data ^= data >> 8;
|
||||
// return !nfc_util_odd_byte_parity[data];
|
||||
return (__builtin_parity(data) & 0xFF);
|
||||
}
|
||||
|
||||
uint8_t nfc_util_odd_parity8(uint8_t data) {
|
||||
return nfc_util_odd_byte_parity[data];
|
||||
}
|
||||
11
lib/nfc/protocols/nfc_util.h
Normal file
11
lib/nfc/protocols/nfc_util.h
Normal file
@@ -0,0 +1,11 @@
|
||||
#pragma once
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
void nfc_util_num2bytes(uint64_t src, uint8_t len, uint8_t* dest);
|
||||
|
||||
uint64_t nfc_util_bytes2num(uint8_t* src, uint8_t len);
|
||||
|
||||
uint8_t nfc_util_even_parity32(uint32_t data);
|
||||
|
||||
uint8_t nfc_util_odd_parity8(uint8_t data);
|
||||
142
lib/nfc/protocols/nfca.c
Executable file
142
lib/nfc/protocols/nfca.c
Executable file
@@ -0,0 +1,142 @@
|
||||
#include "nfca.h"
|
||||
#include <string.h>
|
||||
#include <stdio.h>
|
||||
#include <furi.h>
|
||||
|
||||
#define NFCA_CMD_RATS (0xE0U)
|
||||
|
||||
#define NFCA_CRC_INIT (0x6363)
|
||||
|
||||
#define NFCA_F_SIG (13560000.0)
|
||||
#define T_SIG 7374 //73.746ns*100
|
||||
#define T_SIG_x8 58992 //T_SIG*8
|
||||
#define T_SIG_x8_x8 471936 //T_SIG*8*8
|
||||
#define T_SIG_x8_x9 530928 //T_SIG*8*9
|
||||
|
||||
#define NFCA_SIGNAL_MAX_EDGES (1350)
|
||||
|
||||
typedef struct {
|
||||
uint8_t cmd;
|
||||
uint8_t param;
|
||||
} nfca_cmd_rats;
|
||||
|
||||
static uint8_t nfca_default_ats[] = {0x05, 0x78, 0x80, 0x80, 0x00};
|
||||
|
||||
static uint8_t nfca_sleep_req[] = {0x50, 0x00};
|
||||
|
||||
uint16_t nfca_get_crc16(uint8_t* buff, uint16_t len) {
|
||||
uint16_t crc = NFCA_CRC_INIT;
|
||||
uint8_t byte = 0;
|
||||
|
||||
for(uint8_t i = 0; i < len; i++) {
|
||||
byte = buff[i];
|
||||
byte ^= (uint8_t)(crc & 0xff);
|
||||
byte ^= byte << 4;
|
||||
crc = (crc >> 8) ^ (((uint16_t)byte) << 8) ^ (((uint16_t)byte) << 3) ^
|
||||
(((uint16_t)byte) >> 4);
|
||||
}
|
||||
|
||||
return crc;
|
||||
}
|
||||
|
||||
void nfca_append_crc16(uint8_t* buff, uint16_t len) {
|
||||
uint16_t crc = nfca_get_crc16(buff, len);
|
||||
buff[len] = (uint8_t)crc;
|
||||
buff[len + 1] = (uint8_t)(crc >> 8);
|
||||
}
|
||||
|
||||
bool nfca_emulation_handler(
|
||||
uint8_t* buff_rx,
|
||||
uint16_t buff_rx_len,
|
||||
uint8_t* buff_tx,
|
||||
uint16_t* buff_tx_len) {
|
||||
bool sleep = false;
|
||||
uint8_t rx_bytes = buff_rx_len / 8;
|
||||
|
||||
if(rx_bytes == sizeof(nfca_sleep_req) && !memcmp(buff_rx, nfca_sleep_req, rx_bytes)) {
|
||||
sleep = true;
|
||||
} else if(rx_bytes == sizeof(nfca_cmd_rats) && buff_rx[0] == NFCA_CMD_RATS) {
|
||||
memcpy(buff_tx, nfca_default_ats, sizeof(nfca_default_ats));
|
||||
*buff_tx_len = sizeof(nfca_default_ats) * 8;
|
||||
}
|
||||
|
||||
return sleep;
|
||||
}
|
||||
|
||||
static void nfca_add_bit(DigitalSignal* signal, bool bit) {
|
||||
if(bit) {
|
||||
signal->start_level = true;
|
||||
for(size_t i = 0; i < 7; i++) {
|
||||
signal->edge_timings[i] = T_SIG_x8;
|
||||
}
|
||||
signal->edge_timings[7] = T_SIG_x8_x9;
|
||||
signal->edge_cnt = 8;
|
||||
} else {
|
||||
signal->start_level = false;
|
||||
signal->edge_timings[0] = T_SIG_x8_x8;
|
||||
for(size_t i = 1; i < 9; i++) {
|
||||
signal->edge_timings[i] = T_SIG_x8;
|
||||
}
|
||||
signal->edge_cnt = 9;
|
||||
}
|
||||
}
|
||||
|
||||
static void nfca_add_byte(NfcaSignal* nfca_signal, uint8_t byte, bool parity) {
|
||||
for(uint8_t i = 0; i < 8; i++) {
|
||||
if(byte & (1 << i)) {
|
||||
digital_signal_append(nfca_signal->tx_signal, nfca_signal->one);
|
||||
} else {
|
||||
digital_signal_append(nfca_signal->tx_signal, nfca_signal->zero);
|
||||
}
|
||||
}
|
||||
if(parity) {
|
||||
digital_signal_append(nfca_signal->tx_signal, nfca_signal->one);
|
||||
} else {
|
||||
digital_signal_append(nfca_signal->tx_signal, nfca_signal->zero);
|
||||
}
|
||||
}
|
||||
|
||||
NfcaSignal* nfca_signal_alloc() {
|
||||
NfcaSignal* nfca_signal = malloc(sizeof(NfcaSignal));
|
||||
nfca_signal->one = digital_signal_alloc(10);
|
||||
nfca_signal->zero = digital_signal_alloc(10);
|
||||
nfca_add_bit(nfca_signal->one, true);
|
||||
nfca_add_bit(nfca_signal->zero, false);
|
||||
nfca_signal->tx_signal = digital_signal_alloc(NFCA_SIGNAL_MAX_EDGES);
|
||||
|
||||
return nfca_signal;
|
||||
}
|
||||
|
||||
void nfca_signal_free(NfcaSignal* nfca_signal) {
|
||||
furi_assert(nfca_signal);
|
||||
|
||||
digital_signal_free(nfca_signal->one);
|
||||
digital_signal_free(nfca_signal->zero);
|
||||
digital_signal_free(nfca_signal->tx_signal);
|
||||
free(nfca_signal);
|
||||
}
|
||||
|
||||
void nfca_signal_encode(NfcaSignal* nfca_signal, uint8_t* data, uint16_t bits, uint8_t* parity) {
|
||||
furi_assert(nfca_signal);
|
||||
furi_assert(data);
|
||||
furi_assert(parity);
|
||||
|
||||
nfca_signal->tx_signal->edge_cnt = 0;
|
||||
nfca_signal->tx_signal->start_level = true;
|
||||
// Start of frame
|
||||
digital_signal_append(nfca_signal->tx_signal, nfca_signal->one);
|
||||
|
||||
if(bits < 8) {
|
||||
for(size_t i = 0; i < bits; i++) {
|
||||
if(FURI_BIT(data[0], i)) {
|
||||
digital_signal_append(nfca_signal->tx_signal, nfca_signal->one);
|
||||
} else {
|
||||
digital_signal_append(nfca_signal->tx_signal, nfca_signal->zero);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
for(size_t i = 0; i < bits / 8; i++) {
|
||||
nfca_add_byte(nfca_signal, data[i], parity[i / 8] & (1 << (7 - (i & 0x07))));
|
||||
}
|
||||
}
|
||||
}
|
||||
28
lib/nfc/protocols/nfca.h
Normal file
28
lib/nfc/protocols/nfca.h
Normal file
@@ -0,0 +1,28 @@
|
||||
#pragma once
|
||||
|
||||
#include <stdint.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
#include <lib/digital_signal/digital_signal.h>
|
||||
|
||||
typedef struct {
|
||||
DigitalSignal* one;
|
||||
DigitalSignal* zero;
|
||||
DigitalSignal* tx_signal;
|
||||
} NfcaSignal;
|
||||
|
||||
uint16_t nfca_get_crc16(uint8_t* buff, uint16_t len);
|
||||
|
||||
void nfca_append_crc16(uint8_t* buff, uint16_t len);
|
||||
|
||||
bool nfca_emulation_handler(
|
||||
uint8_t* buff_rx,
|
||||
uint16_t buff_rx_len,
|
||||
uint8_t* buff_tx,
|
||||
uint16_t* buff_tx_len);
|
||||
|
||||
NfcaSignal* nfca_signal_alloc();
|
||||
|
||||
void nfca_signal_free(NfcaSignal* nfca_signal);
|
||||
|
||||
void nfca_signal_encode(NfcaSignal* nfca_signal, uint8_t* data, uint16_t bits, uint8_t* parity);
|
||||
Reference in New Issue
Block a user