[FL-1396] Mifare Classic read (#1034)

* rfal: add new data exchange function
* core: add FURI_BIT to common defines
* furi_hal_nfc: add data exchange with custom patiry bits
* lib: extend nfc common API
* assets: add mf classic dictionary
* lib: introduce mifare classic library
* nfc: add dictionary reader helper
* nfc worker: add worker events, add mifare classic read
* nfc: rework scenes with worker events
* nfc: add read mifare classic GUI
* nfc device: add mifare classic save
* nfc: add dictionary open fail scene
* nfc: mention resources
* stream: fix stream read line
* subghz: rework file read with fixed stream_read_line
* furi_hal_nfc: decrease communication timeout
* nfc: rework keys load from dictionary with file_stream
* nfc: add read mifare classic suggestion
* nfc: fix mifare classic read view
* nfc: fix index size
* nfc: add switch to no dictionary found scene
* nfc: add mifare classic load
* nfc: improve read mifare classic design
* mifare_classic: add proxmark3 mention
* nfc: format sources
* nfc: fix typos, add documentation
This commit is contained in:
gornekich 2022-03-24 01:14:34 +03:00 committed by GitHub
parent 46a894bc5c
commit eafeefb843
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
46 changed files with 3113 additions and 111 deletions

View File

@ -8,4 +8,5 @@ enum NfcCustomEvent {
NfcCustomEventWorkerExit,
NfcCustomEventByteInputDone,
NfcCustomEventTextInputDone,
NfcCustomEventDictAttackDone,
};

View File

@ -0,0 +1,53 @@
#include "nfc_mf_classic_dict.h"
#include <flipper_format/flipper_format.h>
#include <lib/toolbox/args.h>
#define NFC_MF_CLASSIC_DICT_PATH "/ext/nfc/assets/mf_classic_dict.nfc"
#define NFC_MF_CLASSIC_KEY_LEN (13)
bool nfc_mf_classic_dict_check_presence(Storage* storage) {
furi_assert(storage);
return storage_common_stat(storage, NFC_MF_CLASSIC_DICT_PATH, NULL) == FSE_OK;
}
bool nfc_mf_classic_dict_open_file(Stream* stream) {
furi_assert(stream);
return file_stream_open(stream, NFC_MF_CLASSIC_DICT_PATH, FSAM_READ, FSOM_OPEN_EXISTING);
}
void nfc_mf_classic_dict_close_file(Stream* stream) {
furi_assert(stream);
file_stream_close(stream);
}
bool nfc_mf_classic_dict_get_next_key(Stream* stream, uint64_t* key) {
furi_assert(stream);
furi_assert(key);
uint8_t key_byte_tmp = 0;
string_t next_line;
string_init(next_line);
*key = 0;
bool next_key_read = false;
while(!next_key_read) {
if(stream_read_line(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);
}
next_key_read = true;
}
string_clear(next_line);
return next_key_read;
}
void nfc_mf_classic_dict_reset(Stream* stream) {
furi_assert(stream);
stream_rewind(stream);
}

View File

@ -0,0 +1,15 @@
#pragma once
#include <stdbool.h>
#include <storage/storage.h>
#include <lib/toolbox/stream/file_stream.h>
bool nfc_mf_classic_dict_check_presence(Storage* storage);
bool nfc_mf_classic_dict_open_file(Stream* stream);
void nfc_mf_classic_dict_close_file(Stream* stream);
bool nfc_mf_classic_dict_get_next_key(Stream* stream, uint64_t* key);
void nfc_mf_classic_dict_reset(Stream* stream);

View File

@ -79,6 +79,11 @@ Nfc* nfc_alloc() {
view_dispatcher_add_view(
nfc->view_dispatcher, NfcViewBankCard, bank_card_get_view(nfc->bank_card));
// Dict Attack
nfc->dict_attack = dict_attack_alloc();
view_dispatcher_add_view(
nfc->view_dispatcher, NfcViewDictAttack, dict_attack_get_view(nfc->dict_attack));
return nfc;
}
@ -121,6 +126,10 @@ void nfc_free(Nfc* nfc) {
view_dispatcher_remove_view(nfc->view_dispatcher, NfcViewBankCard);
bank_card_free(nfc->bank_card);
// Dict Attack
view_dispatcher_remove_view(nfc->view_dispatcher, NfcViewDictAttack);
dict_attack_free(nfc->dict_attack);
// Worker
nfc_worker_stop(nfc->worker);
nfc_worker_free(nfc->worker);

View File

@ -22,13 +22,15 @@ void nfc_device_free(NfcDevice* nfc_dev) {
free(nfc_dev);
}
void nfc_device_prepare_format_string(NfcDevice* dev, string_t format_string) {
static void nfc_device_prepare_format_string(NfcDevice* dev, string_t format_string) {
if(dev->format == NfcDeviceSaveFormatUid) {
string_set_str(format_string, "UID");
} else if(dev->format == NfcDeviceSaveFormatBankCard) {
string_set_str(format_string, "Bank card");
} else if(dev->format == NfcDeviceSaveFormatMifareUl) {
string_set_str(format_string, nfc_mf_ul_type(dev->dev_data.mf_ul_data.type, true));
} else if(dev->format == NfcDeviceSaveFormatMifareClassic) {
string_set_str(format_string, "Mifare Classic");
} else if(dev->format == NfcDeviceSaveFormatMifareDesfire) {
string_set_str(format_string, "Mifare DESFire");
} else {
@ -36,7 +38,7 @@ void nfc_device_prepare_format_string(NfcDevice* dev, string_t format_string) {
}
}
bool nfc_device_parse_format_string(NfcDevice* dev, string_t format_string) {
static bool nfc_device_parse_format_string(NfcDevice* dev, string_t format_string) {
if(string_start_with_str_p(format_string, "UID")) {
dev->format = NfcDeviceSaveFormatUid;
dev->dev_data.nfc_data.protocol = NfcDeviceProtocolUnknown;
@ -56,6 +58,11 @@ bool nfc_device_parse_format_string(NfcDevice* dev, string_t format_string) {
return true;
}
}
if(string_start_with_str_p(format_string, "Mifare Classic")) {
dev->format = NfcDeviceSaveFormatMifareClassic;
dev->dev_data.nfc_data.protocol = NfcDeviceProtocolMifareClassic;
return true;
}
if(string_start_with_str_p(format_string, "Mifare DESFire")) {
dev->format = NfcDeviceSaveFormatMifareDesfire;
dev->dev_data.nfc_data.protocol = NfcDeviceProtocolMifareDesfire;
@ -605,6 +612,79 @@ bool nfc_device_load_bank_card_data(FlipperFormat* file, NfcDevice* dev) {
return parsed;
}
static bool nfc_device_save_mifare_classic_data(FlipperFormat* file, NfcDevice* dev) {
bool saved = false;
MfClassicData* data = &dev->dev_data.mf_classic_data;
string_t temp_str;
string_init(temp_str);
uint16_t blocks = 0;
// Save Mifare Classic specific data
do {
if(!flipper_format_write_comment_cstr(file, "Mifare Classic specific data")) break;
if(data->type == MfClassicType1k) {
if(!flipper_format_write_string_cstr(file, "Mifare Classic type", "1K")) break;
blocks = 64;
} else if(data->type == MfClassicType4k) {
if(!flipper_format_write_string_cstr(file, "Mifare Classic type", "4K")) break;
blocks = 256;
}
if(!flipper_format_write_comment_cstr(file, "Mifare Classic blocks")) break;
bool block_saved = true;
for(size_t i = 0; i < blocks; i++) {
string_printf(temp_str, "Block %d", i);
if(!flipper_format_write_hex(
file, string_get_cstr(temp_str), data->block[i].value, 16)) {
block_saved = false;
break;
}
}
if(!block_saved) break;
saved = true;
} while(false);
string_clear(temp_str);
return saved;
}
static bool nfc_device_load_mifare_classic_data(FlipperFormat* file, NfcDevice* dev) {
bool parsed = false;
MfClassicData* data = &dev->dev_data.mf_classic_data;
string_t temp_str;
string_init(temp_str);
uint16_t data_blocks = 0;
do {
// Read Mifare Classic type
if(!flipper_format_read_string(file, "Mifare Classic type", temp_str)) break;
if(!string_cmp_str(temp_str, "1K")) {
data->type = MfClassicType1k;
data_blocks = 64;
} else if(!string_cmp_str(temp_str, "4K")) {
data->type = MfClassicType4k;
data_blocks = 256;
} else {
break;
}
// Read Mifare Classic blocks
bool block_read = true;
for(size_t i = 0; i < data_blocks; i++) {
string_printf(temp_str, "Block %d", i);
if(!flipper_format_read_hex(
file, string_get_cstr(temp_str), data->block[i].value, 16)) {
block_read = false;
break;
}
}
if(!block_read) break;
parsed = true;
} while(false);
string_clear(temp_str);
return parsed;
}
void nfc_device_set_name(NfcDevice* dev, const char* name) {
furi_assert(dev);
@ -635,7 +715,7 @@ static bool nfc_device_save_file(
if(!flipper_format_write_header_cstr(file, nfc_file_header, nfc_file_version)) break;
// Write nfc device type
if(!flipper_format_write_comment_cstr(
file, "Nfc device type can be UID, Mifare Ultralight, Bank card"))
file, "Nfc device type can be UID, Mifare Ultralight, Mifare Classic, Bank card"))
break;
nfc_device_prepare_format_string(dev, temp_str);
if(!flipper_format_write_string(file, "Device type", temp_str)) break;
@ -652,6 +732,8 @@ static bool nfc_device_save_file(
if(!nfc_device_save_mifare_df_data(file, dev)) break;
} else if(dev->format == NfcDeviceSaveFormatBankCard) {
if(!nfc_device_save_bank_card_data(file, dev)) break;
} else if(dev->format == NfcDeviceSaveFormatMifareClassic) {
if(!nfc_device_save_mifare_classic_data(file, dev)) break;
}
saved = true;
} while(0);
@ -714,6 +796,8 @@ static bool nfc_device_load_data(NfcDevice* dev, string_t path) {
// Parse other data
if(dev->format == NfcDeviceSaveFormatMifareUl) {
if(!nfc_device_load_mifare_ul_data(file, dev)) break;
} else if(dev->format == NfcDeviceSaveFormatMifareClassic) {
if(!nfc_device_load_mifare_classic_data(file, dev)) break;
} else if(dev->format == NfcDeviceSaveFormatMifareDesfire) {
if(!nfc_device_load_mifare_df_data(file, dev)) break;
} else if(dev->format == NfcDeviceSaveFormatBankCard) {

View File

@ -5,8 +5,9 @@
#include <storage/storage.h>
#include <dialogs/dialogs.h>
#include "mifare_ultralight.h"
#include "mifare_desfire.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_FILE_NAME_MAX_LEN 120
@ -27,6 +28,7 @@ typedef enum {
NfcDeviceProtocolUnknown,
NfcDeviceProtocolEMV,
NfcDeviceProtocolMifareUl,
NfcDeviceProtocolMifareClassic,
NfcDeviceProtocolMifareDesfire,
} NfcProtocol;
@ -34,6 +36,7 @@ typedef enum {
NfcDeviceSaveFormatUid,
NfcDeviceSaveFormatBankCard,
NfcDeviceSaveFormatMifareUl,
NfcDeviceSaveFormatMifareClassic,
NfcDeviceSaveFormatMifareDesfire,
} NfcDeviceSaveFormat;
@ -69,6 +72,7 @@ typedef struct {
union {
NfcEmvData emv_data;
MifareUlData mf_ul_data;
MfClassicData mf_classic_data;
MifareDesfireData mf_df_data;
};
} NfcDeviceData;

View File

@ -24,6 +24,7 @@
#include <gui/modules/widget.h>
#include "views/bank_card.h"
#include "views/dict_attack.h"
#include <nfc/scenes/nfc_scene.h>
#include <nfc/helpers/nfc_custom_event.h>
@ -53,6 +54,7 @@ struct Nfc {
TextBox* text_box;
Widget* widget;
BankCard* bank_card;
DictAttack* dict_attack;
};
typedef enum {
@ -64,6 +66,7 @@ typedef enum {
NfcViewTextBox,
NfcViewWidget,
NfcViewBankCard,
NfcViewDictAttack,
} NfcView;
Nfc* nfc_alloc();

View File

@ -53,6 +53,8 @@ const char* nfc_guess_protocol(NfcProtocol protocol) {
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 {
@ -75,3 +77,13 @@ const char* nfc_mf_ul_type(MfUltralightType type, bool full_name) {
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";
}
}

View File

@ -15,3 +15,5 @@ const char* nfc_get_nfca_type(rfalNfcaListenDeviceType 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);

View File

@ -1,8 +1,13 @@
#include "nfc_worker_i.h"
#include <furi_hal.h>
#include "nfc_protocols/emv_decoder.h"
#include "nfc_protocols/mifare_desfire.h"
#include "nfc_protocols/mifare_ultralight.h"
#include <lib/nfc_protocols/nfc_util.h>
#include <lib/nfc_protocols/emv_decoder.h>
#include <lib/nfc_protocols/mifare_ultralight.h>
#include <lib/nfc_protocols/mifare_classic.h>
#include <lib/nfc_protocols/mifare_desfire.h>
#include "helpers/nfc_mf_classic_dict.h"
#define TAG "NfcWorker"
@ -20,6 +25,7 @@ NfcWorker* nfc_worker_alloc() {
nfc_worker->callback = NULL;
nfc_worker->context = NULL;
nfc_worker->storage = furi_record_open("storage");
// Initialize rfal
while(furi_hal_nfc_is_busy()) {
@ -33,6 +39,7 @@ NfcWorker* nfc_worker_alloc() {
void nfc_worker_free(NfcWorker* nfc_worker) {
furi_assert(nfc_worker);
furi_thread_free(nfc_worker->thread);
furi_record_close("storage");
free(nfc_worker);
}
@ -95,6 +102,8 @@ int32_t nfc_worker_task(void* context) {
nfc_worker_read_mifare_ul(nfc_worker);
} else if(nfc_worker->state == NfcWorkerStateEmulateMifareUl) {
nfc_worker_emulate_mifare_ul(nfc_worker);
} else if(nfc_worker->state == NfcWorkerStateReadMifareClassic) {
nfc_worker_mifare_classic_dict_attack(nfc_worker);
} else if(nfc_worker->state == NfcWorkerStateReadMifareDesfire) {
nfc_worker_read_mifare_desfire(nfc_worker);
} else if(nfc_worker->state == NfcWorkerStateField) {
@ -130,6 +139,11 @@ void nfc_worker_detect(NfcWorker* nfc_worker) {
dev->dev.nfca.sensRes.platformInfo,
dev->dev.nfca.selRes.sak)) {
result->protocol = NfcDeviceProtocolMifareUl;
} else if(mf_classic_check_card_type(
dev->dev.nfca.sensRes.anticollisionInfo,
dev->dev.nfca.sensRes.platformInfo,
dev->dev.nfca.selRes.sak)) {
result->protocol = NfcDeviceProtocolMifareClassic;
} else if(mf_df_check_card_type(
dev->dev.nfca.sensRes.anticollisionInfo,
dev->dev.nfca.sensRes.platformInfo,
@ -149,7 +163,7 @@ void nfc_worker_detect(NfcWorker* nfc_worker) {
}
// Notify caller and exit
if(nfc_worker->callback) {
nfc_worker->callback(nfc_worker->context);
nfc_worker->callback(NfcWorkerEventSuccess, nfc_worker->context);
}
break;
}
@ -171,7 +185,7 @@ bool nfc_worker_emulate_uid_callback(
if(reader_data->size > 0) {
memcpy(reader_data->data, buff_rx, reader_data->size);
if(nfc_worker->callback) {
nfc_worker->callback(nfc_worker->context);
nfc_worker->callback(NfcWorkerEventSuccess, nfc_worker->context);
}
}
return true;
@ -231,7 +245,7 @@ void nfc_worker_read_emv_app(NfcWorker* nfc_worker) {
result->emv_data.aid_len = emv_app.aid_len;
memcpy(result->emv_data.aid, emv_app.aid, emv_app.aid_len);
if(nfc_worker->callback) {
nfc_worker->callback(nfc_worker->context);
nfc_worker->callback(NfcWorkerEventSuccess, nfc_worker->context);
}
break;
} else {
@ -329,7 +343,7 @@ void nfc_worker_read_emv(NfcWorker* nfc_worker) {
memcpy(result->emv_data.number, emv_app.card_number, emv_app.card_number_len);
// Notify caller and exit
if(nfc_worker->callback) {
nfc_worker->callback(nfc_worker->context);
nfc_worker->callback(NfcWorkerEventSuccess, nfc_worker->context);
}
break;
} else {
@ -378,7 +392,7 @@ void nfc_worker_read_emv(NfcWorker* nfc_worker) {
}
// Notify caller and exit
if(nfc_worker->callback) {
nfc_worker->callback(nfc_worker->context);
nfc_worker->callback(NfcWorkerEventSuccess, nfc_worker->context);
}
break;
} else {
@ -633,7 +647,7 @@ void nfc_worker_read_mifare_ul(NfcWorker* nfc_worker) {
// Notify caller and exit
if(nfc_worker->callback) {
nfc_worker->callback(nfc_worker->context);
nfc_worker->callback(NfcWorkerEventSuccess, nfc_worker->context);
}
break;
} else {
@ -663,13 +677,166 @@ void nfc_worker_emulate_mifare_ul(NfcWorker* nfc_worker) {
if(mf_ul_emulate.data_changed) {
nfc_worker->dev_data->mf_ul_data = mf_ul_emulate.data;
if(nfc_worker->callback) {
nfc_worker->callback(nfc_worker->context);
nfc_worker->callback(NfcWorkerEventSuccess, nfc_worker->context);
}
mf_ul_emulate.data_changed = false;
}
}
}
void nfc_worker_mifare_classic_dict_attack(NfcWorker* nfc_worker) {
furi_assert(nfc_worker->callback);
rfalNfcDevice* dev_list;
rfalNfcDevice* dev;
NfcDeviceCommonData* nfc_common;
uint8_t dev_cnt = 0;
FuriHalNfcTxRxContext tx_rx_ctx = {};
MfClassicAuthContext auth_ctx = {};
MfClassicReader reader = {};
uint64_t curr_key = 0;
uint16_t curr_sector = 0;
uint8_t total_sectors = 0;
NfcWorkerEvent event;
// Open dictionary
nfc_worker->dict_stream = file_stream_alloc(nfc_worker->storage);
if(!nfc_mf_classic_dict_open_file(nfc_worker->dict_stream)) {
event = NfcWorkerEventNoDictFound;
nfc_worker->callback(event, nfc_worker->context);
nfc_mf_classic_dict_close_file(nfc_worker->dict_stream);
stream_free(nfc_worker->dict_stream);
return;
}
// Detect Mifare Classic card
while(nfc_worker->state == NfcWorkerStateReadMifareClassic) {
if(furi_hal_nfc_detect(&dev_list, &dev_cnt, 300, false)) {
dev = &dev_list[0];
if(mf_classic_get_type(
dev->nfcid,
dev->nfcidLen,
dev->dev.nfca.sensRes.anticollisionInfo,
dev->dev.nfca.sensRes.platformInfo,
dev->dev.nfca.selRes.sak,
&reader)) {
total_sectors = mf_classic_get_total_sectors_num(&reader);
if(reader.type == MfClassicType1k) {
event = NfcWorkerEventDetectedClassic1k;
} else {
event = NfcWorkerEventDetectedClassic4k;
}
nfc_worker->callback(event, nfc_worker->context);
break;
}
} else {
event = NfcWorkerEventNoCardDetected;
nfc_worker->callback(event, nfc_worker->context);
}
}
if(nfc_worker->state == NfcWorkerStateReadMifareClassic) {
bool card_removed_notified = false;
bool card_found_notified = false;
// Seek for mifare classic keys
for(curr_sector = 0; curr_sector < total_sectors; curr_sector++) {
FURI_LOG_I(TAG, "Sector: %d ...", curr_sector);
event = NfcWorkerEventNewSector;
nfc_worker->callback(event, nfc_worker->context);
mf_classic_auth_init_context(&auth_ctx, reader.cuid, curr_sector);
bool sector_key_found = false;
while(nfc_mf_classic_dict_get_next_key(nfc_worker->dict_stream, &curr_key)) {
furi_hal_nfc_deactivate();
if(furi_hal_nfc_activate_nfca(300, &reader.cuid)) {
if(!card_found_notified) {
if(reader.type == MfClassicType1k) {
event = NfcWorkerEventDetectedClassic1k;
} else {
event = NfcWorkerEventDetectedClassic4k;
}
nfc_worker->callback(event, 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",
curr_sector,
(uint32_t)(curr_key >> 32),
(uint32_t)curr_key);
if(mf_classic_auth_attempt(&tx_rx_ctx, &auth_ctx, curr_key)) {
sector_key_found = true;
if((auth_ctx.key_a != MF_CLASSIC_NO_KEY) &&
(auth_ctx.key_b != MF_CLASSIC_NO_KEY))
break;
}
} else {
// Notify that no tag is availalble
FURI_LOG_D(TAG, "Can't find tags");
if(!card_removed_notified) {
event = NfcWorkerEventNoCardDetected;
nfc_worker->callback(event, nfc_worker->context);
card_removed_notified = true;
card_found_notified = false;
}
}
if(nfc_worker->state != NfcWorkerStateReadMifareClassic) break;
}
if(nfc_worker->state != NfcWorkerStateReadMifareClassic) break;
if(sector_key_found) {
// Notify that keys were found
if(auth_ctx.key_a != MF_CLASSIC_NO_KEY) {
FURI_LOG_I(
TAG,
"Sector %d key A: %04lx%08lx",
curr_sector,
(uint32_t)(auth_ctx.key_a >> 32),
(uint32_t)auth_ctx.key_a);
event = NfcWorkerEventFoundKeyA;
nfc_worker->callback(event, nfc_worker->context);
}
if(auth_ctx.key_b != MF_CLASSIC_NO_KEY) {
FURI_LOG_I(
TAG,
"Sector %d key B: %04lx%08lx",
curr_sector,
(uint32_t)(auth_ctx.key_b >> 32),
(uint32_t)auth_ctx.key_b);
event = NfcWorkerEventFoundKeyB;
nfc_worker->callback(event, nfc_worker->context);
}
// Add sectors to read sequence
mf_classic_reader_add_sector(&reader, curr_sector, auth_ctx.key_a, auth_ctx.key_b);
}
nfc_mf_classic_dict_reset(nfc_worker->dict_stream);
}
}
if(nfc_worker->state == NfcWorkerStateReadMifareClassic) {
FURI_LOG_I(TAG, "Found keys to %d sectors. Start reading sectors", reader.sectors_to_read);
uint8_t sectors_read =
mf_classic_read_card(&tx_rx_ctx, &reader, &nfc_worker->dev_data->mf_classic_data);
if(sectors_read) {
dev = &dev_list[0];
nfc_common = &nfc_worker->dev_data->nfc_data;
nfc_common->uid_len = dev->dev.nfca.nfcId1Len;
nfc_common->atqa[0] = dev->dev.nfca.sensRes.anticollisionInfo;
nfc_common->atqa[1] = dev->dev.nfca.sensRes.platformInfo;
nfc_common->sak = dev->dev.nfca.selRes.sak;
nfc_common->protocol = NfcDeviceProtocolMifareClassic;
memcpy(nfc_common->uid, dev->dev.nfca.nfcId1, nfc_common->uid_len);
event = NfcWorkerEventSuccess;
FURI_LOG_I(TAG, "Successfully read %d sectors", sectors_read);
} else {
event = NfcWorkerEventFail;
FURI_LOG_W(TAG, "Failed to read any sector");
}
nfc_worker->callback(event, nfc_worker->context);
}
nfc_mf_classic_dict_close_file(nfc_worker->dict_stream);
stream_free(nfc_worker->dict_stream);
}
ReturnCode nfc_exchange_full(
uint8_t* tx_buff,
uint16_t tx_len,
@ -900,7 +1067,7 @@ void nfc_worker_read_mifare_desfire(NfcWorker* nfc_worker) {
// Notify caller and exit
if(nfc_worker->callback) {
nfc_worker->callback(nfc_worker->context);
nfc_worker->callback(NfcWorkerEventSuccess, nfc_worker->context);
}
break;
}

View File

@ -18,12 +18,27 @@ typedef enum {
NfcWorkerStateField,
NfcWorkerStateReadMifareUl,
NfcWorkerStateEmulateMifareUl,
NfcWorkerStateReadMifareClassic,
NfcWorkerStateReadMifareDesfire,
// Transition
NfcWorkerStateStop,
} NfcWorkerState;
typedef void (*NfcWorkerCallback)(void* context);
typedef enum {
NfcWorkerEventSuccess,
NfcWorkerEventFail,
NfcWorkerEventNoCardDetected,
// Mifare Classic events
NfcWorkerEventNoDictFound,
NfcWorkerEventDetectedClassic1k,
NfcWorkerEventDetectedClassic4k,
NfcWorkerEventNewSector,
NfcWorkerEventFoundKeyA,
NfcWorkerEventFoundKeyB,
NfcWorkerEventStartReading,
} NfcWorkerEvent;
typedef void (*NfcWorkerCallback)(NfcWorkerEvent event, void* context);
NfcWorker* nfc_worker_alloc();

View File

@ -5,6 +5,7 @@
#include <furi.h>
#include <stdbool.h>
#include <lib/toolbox/stream/file_stream.h>
#include <rfal_analogConfig.h>
#include <rfal_rf.h>
@ -18,6 +19,8 @@
struct NfcWorker {
FuriThread* thread;
Storage* storage;
Stream* dict_stream;
NfcDeviceData* dev_data;
@ -45,6 +48,10 @@ void nfc_worker_field(NfcWorker* nfc_worker);
void nfc_worker_read_mifare_ul(NfcWorker* nfc_worker);
void nfc_worker_mifare_classic_dict_attack(NfcWorker* nfc_worker);
void nfc_worker_read_mifare_desfire(NfcWorker* nfc_worker);
void nfc_worker_emulate_mifare_ul(NfcWorker* nfc_worker);
void nfc_worker_emulate_mifare_classic(NfcWorker* nfc_worker);

View File

@ -8,13 +8,13 @@ enum SubmenuIndex {
};
void nfc_scene_card_menu_submenu_callback(void* context, uint32_t index) {
Nfc* nfc = (Nfc*)context;
Nfc* nfc = context;
view_dispatcher_send_custom_event(nfc->view_dispatcher, index);
}
void nfc_scene_card_menu_on_enter(void* context) {
Nfc* nfc = (Nfc*)context;
Nfc* nfc = context;
Submenu* submenu = nfc->submenu;
if(nfc->dev->dev_data.nfc_data.protocol > NfcDeviceProtocolUnknown) {
@ -42,7 +42,8 @@ void nfc_scene_card_menu_on_enter(void* context) {
}
bool nfc_scene_card_menu_on_event(void* context, SceneManagerEvent event) {
Nfc* nfc = (Nfc*)context;
Nfc* nfc = context;
bool consumed = false;
if(event.type == SceneManagerEventTypeCustom) {
if(event.event == SubmenuIndexRunApp) {
@ -54,34 +55,36 @@ bool nfc_scene_card_menu_on_event(void* context, SceneManagerEvent event) {
scene_manager_next_scene(nfc->scene_manager, NfcSceneReadMifareDesfire);
} else if(nfc->dev->dev_data.nfc_data.protocol == NfcDeviceProtocolEMV) {
scene_manager_next_scene(nfc->scene_manager, NfcSceneReadEmvApp);
} else if(nfc->dev->dev_data.nfc_data.protocol == NfcDeviceProtocolMifareClassic) {
scene_manager_next_scene(nfc->scene_manager, NfcSceneReadMifareClassic);
}
return true;
consumed = true;
} else if(event.event == SubmenuIndexChooseScript) {
scene_manager_set_scene_state(
nfc->scene_manager, NfcSceneCardMenu, SubmenuIndexChooseScript);
scene_manager_next_scene(nfc->scene_manager, NfcSceneScriptsMenu);
return true;
consumed = true;
} else if(event.event == SubmenuIndexEmulate) {
scene_manager_set_scene_state(
nfc->scene_manager, NfcSceneCardMenu, SubmenuIndexEmulate);
scene_manager_next_scene(nfc->scene_manager, NfcSceneEmulateUid);
return true;
consumed = true;
} else if(event.event == SubmenuIndexSave) {
scene_manager_set_scene_state(nfc->scene_manager, NfcSceneCardMenu, SubmenuIndexSave);
nfc->dev->format = NfcDeviceSaveFormatUid;
scene_manager_next_scene(nfc->scene_manager, NfcSceneSaveName);
return true;
consumed = true;
}
} else if(event.type == SceneManagerEventTypeBack) {
return scene_manager_search_and_switch_to_previous_scene(
nfc->scene_manager, NfcSceneStart);
consumed =
scene_manager_search_and_switch_to_previous_scene(nfc->scene_manager, NfcSceneStart);
}
return false;
return consumed;
}
void nfc_scene_card_menu_on_exit(void* context) {
Nfc* nfc = (Nfc*)context;
Nfc* nfc = context;
submenu_reset(nfc->submenu);
}

View File

@ -34,3 +34,5 @@ ADD_SCENE(nfc, emulate_apdu_sequence, EmulateApduSequence)
ADD_SCENE(nfc, restore_original, RestoreOriginal)
ADD_SCENE(nfc, debug, Debug)
ADD_SCENE(nfc, field, Field)
ADD_SCENE(nfc, read_mifare_classic, ReadMifareClassic)
ADD_SCENE(nfc, dict_not_found, DictNotFound)

View File

@ -30,13 +30,19 @@ void nfc_scene_device_info_bank_card_callback(GuiButtonType result, InputType ty
void nfc_scene_device_info_on_enter(void* context) {
Nfc* nfc = context;
bool data_display_supported = (nfc->dev->format == NfcDeviceSaveFormatUid) ||
(nfc->dev->format == NfcDeviceSaveFormatMifareUl) ||
(nfc->dev->format == NfcDeviceSaveFormatMifareDesfire) ||
(nfc->dev->format == NfcDeviceSaveFormatBankCard);
// Setup Custom Widget view
widget_add_text_box_element(
nfc->widget, 0, 0, 128, 22, AlignCenter, AlignTop, nfc->dev->dev_name);
widget_add_button_element(
nfc->widget, GuiButtonTypeLeft, "Back", nfc_scene_device_info_widget_callback, nfc);
widget_add_button_element(
nfc->widget, GuiButtonTypeRight, "Data", nfc_scene_device_info_widget_callback, nfc);
if(data_display_supported) {
widget_add_button_element(
nfc->widget, GuiButtonTypeRight, "Data", nfc_scene_device_info_widget_callback, nfc);
}
char uid_str[32];
NfcDeviceCommonData* data = &nfc->dev->dev_data.nfc_data;
if(data->uid_len == 4) {
@ -69,6 +75,8 @@ void nfc_scene_device_info_on_enter(void* context) {
protocol_name = nfc_guess_protocol(data->protocol);
} else if(data->protocol == NfcDeviceProtocolMifareUl) {
protocol_name = nfc_mf_ul_type(nfc->dev->dev_data.mf_ul_data.type, false);
} else if(data->protocol == NfcDeviceProtocolMifareClassic) {
protocol_name = nfc_mf_classic_type(nfc->dev->dev_data.mf_classic_data.type);
}
if(protocol_name) {
widget_add_string_element(

View File

@ -0,0 +1,52 @@
#include "../nfc_i.h"
void nfc_scene_dict_not_found_popup_callback(void* context) {
Nfc* nfc = context;
view_dispatcher_send_custom_event(nfc->view_dispatcher, NfcCustomEventViewExit);
}
void nfc_scene_dict_not_found_on_enter(void* context) {
Nfc* nfc = context;
// Setup view
Popup* popup = nfc->popup;
popup_set_text(
popup,
"Function requires\nan SD card with\nfresh databases.",
82,
24,
AlignCenter,
AlignCenter);
popup_set_icon(popup, 6, 10, &I_SDQuestion_35x43);
popup_set_timeout(popup, 2500);
popup_set_context(popup, nfc);
popup_set_callback(popup, nfc_scene_dict_not_found_popup_callback);
popup_enable_timeout(popup);
view_dispatcher_switch_to_view(nfc->view_dispatcher, NfcViewPopup);
}
bool nfc_scene_dict_not_found_on_event(void* context, SceneManagerEvent event) {
Nfc* nfc = context;
bool consumed = false;
if(event.type == SceneManagerEventTypeCustom) {
if(event.event == NfcCustomEventViewExit) {
if(scene_manager_has_previous_scene(nfc->scene_manager, NfcSceneScriptsMenu)) {
consumed = scene_manager_search_and_switch_to_previous_scene(
nfc->scene_manager, NfcSceneScriptsMenu);
} else if(scene_manager_has_previous_scene(nfc->scene_manager, NfcSceneCardMenu)) {
consumed = scene_manager_search_and_switch_to_previous_scene(
nfc->scene_manager, NfcSceneCardMenu);
} else {
consumed = scene_manager_search_and_switch_to_previous_scene(
nfc->scene_manager, NfcSceneStart);
}
}
}
return consumed;
}
void nfc_scene_dict_not_found_on_exit(void* context) {
Nfc* nfc = context;
popup_reset(nfc->popup);
}

View File

@ -4,7 +4,7 @@
#define NFC_MF_UL_DATA_NOT_CHANGED (0UL)
#define NFC_MF_UL_DATA_CHANGED (1UL)
void nfc_emulate_mifare_ul_worker_callback(void* context) {
void nfc_emulate_mifare_ul_worker_callback(NfcWorkerEvent event, void* context) {
Nfc* nfc = (Nfc*)context;
scene_manager_set_scene_state(
nfc->scene_manager, NfcSceneEmulateMifareUl, NFC_MF_UL_DATA_CHANGED);

View File

@ -6,7 +6,7 @@ enum {
NfcSceneEmulateUidStateTextBox,
};
void nfc_emulate_uid_worker_callback(void* context) {
void nfc_emulate_uid_worker_callback(NfcWorkerEvent event, void* context) {
furi_assert(context);
Nfc* nfc = context;
view_dispatcher_send_custom_event(nfc->view_dispatcher, NfcCustomEventWorkerExit);

View File

@ -1,7 +1,7 @@
#include "../nfc_i.h"
#include <dolphin/dolphin.h>
void nfc_read_card_worker_callback(void* context) {
void nfc_read_card_worker_callback(NfcWorkerEvent event, void* context) {
Nfc* nfc = (Nfc*)context;
view_dispatcher_send_custom_event(nfc->view_dispatcher, NfcCustomEventWorkerExit);
}

View File

@ -1,7 +1,7 @@
#include "../nfc_i.h"
#include <dolphin/dolphin.h>
void nfc_read_emv_app_worker_callback(void* context) {
void nfc_read_emv_app_worker_callback(NfcWorkerEvent event, void* context) {
Nfc* nfc = (Nfc*)context;
view_dispatcher_send_custom_event(nfc->view_dispatcher, NfcCustomEventWorkerExit);
}

View File

@ -1,7 +1,7 @@
#include "../nfc_i.h"
#include <dolphin/dolphin.h>
void nfc_read_emv_data_worker_callback(void* context) {
void nfc_read_emv_data_worker_callback(NfcWorkerEvent event, void* context) {
Nfc* nfc = (Nfc*)context;
view_dispatcher_send_custom_event(nfc->view_dispatcher, NfcCustomEventWorkerExit);
}

View File

@ -0,0 +1,95 @@
#include "../nfc_i.h"
enum {
NfcSceneReadMifareClassicStateInProgress,
NfcSceneReadMifareClassicStateDone,
};
void nfc_read_mifare_classic_worker_callback(NfcWorkerEvent event, void* context) {
furi_assert(context);
Nfc* nfc = context;
view_dispatcher_send_custom_event(nfc->view_dispatcher, event);
}
void nfc_read_mifare_classic_dict_attack_result_callback(void* context) {
furi_assert(context);
Nfc* nfc = context;
view_dispatcher_send_custom_event(nfc->view_dispatcher, NfcCustomEventDictAttackDone);
}
void nfc_scene_read_mifare_classic_on_enter(void* context) {
Nfc* nfc = context;
// Setup and start worker
memset(&nfc->dev->dev_data.mf_classic_data, 0, sizeof(MfClassicData));
dict_attack_set_result_callback(
nfc->dict_attack, nfc_read_mifare_classic_dict_attack_result_callback, nfc);
scene_manager_set_scene_state(
nfc->scene_manager, NfcSceneReadMifareClassic, NfcSceneReadMifareClassicStateInProgress);
view_dispatcher_switch_to_view(nfc->view_dispatcher, NfcViewDictAttack);
nfc_worker_start(
nfc->worker,
NfcWorkerStateReadMifareClassic,
&nfc->dev->dev_data,
nfc_read_mifare_classic_worker_callback,
nfc);
}
bool nfc_scene_read_mifare_classic_on_event(void* context, SceneManagerEvent event) {
Nfc* nfc = context;
bool consumed = false;
uint32_t state = scene_manager_get_scene_state(nfc->scene_manager, NfcSceneReadMifareClassic);
if(event.type == SceneManagerEventTypeTick) {
if(state == NfcSceneReadMifareClassicStateInProgress) {
notification_message(nfc->notifications, &sequence_blink_blue_10);
}
consumed = true;
} else if(event.type == SceneManagerEventTypeCustom) {
if(event.event == NfcCustomEventDictAttackDone) {
scene_manager_next_scene(nfc->scene_manager, NfcSceneSaveName);
consumed = true;
} else if(event.event == NfcWorkerEventDetectedClassic1k) {
dict_attack_card_detected(nfc->dict_attack, MfClassicType1k);
consumed = true;
} else if(event.event == NfcWorkerEventDetectedClassic4k) {
dict_attack_card_detected(nfc->dict_attack, MfClassicType4k);
consumed = true;
} else if(event.event == NfcWorkerEventNewSector) {
dict_attack_inc_curr_sector(nfc->dict_attack);
consumed = true;
} else if(event.event == NfcWorkerEventFoundKeyA) {
dict_attack_inc_found_key(nfc->dict_attack, MfClassicKeyA);
consumed = true;
} else if(event.event == NfcWorkerEventFoundKeyB) {
dict_attack_inc_found_key(nfc->dict_attack, MfClassicKeyB);
consumed = true;
} else if(event.event == NfcWorkerEventNoCardDetected) {
dict_attack_card_removed(nfc->dict_attack);
consumed = true;
} else if(event.event == NfcWorkerEventSuccess) {
scene_manager_set_scene_state(
nfc->scene_manager, NfcSceneReadMifareClassic, NfcSceneReadMifareClassicStateDone);
notification_message(nfc->notifications, &sequence_success);
nfc->dev->format = NfcDeviceSaveFormatMifareClassic;
dict_attack_set_result(nfc->dict_attack, true);
consumed = true;
} else if(event.event == NfcWorkerEventFail) {
scene_manager_set_scene_state(
nfc->scene_manager, NfcSceneReadMifareClassic, NfcSceneReadMifareClassicStateDone);
dict_attack_set_result(nfc->dict_attack, false);
consumed = true;
} else if(event.event == NfcWorkerEventNoDictFound) {
scene_manager_next_scene(nfc->scene_manager, NfcSceneDictNotFound);
consumed = true;
}
}
return consumed;
}
void nfc_scene_read_mifare_classic_on_exit(void* context) {
Nfc* nfc = context;
// Stop worker
nfc_worker_stop(nfc->worker);
dict_attack_reset(nfc->dict_attack);
}

View File

@ -1,7 +1,7 @@
#include "../nfc_i.h"
#include <dolphin/dolphin.h>
void nfc_read_mifare_desfire_worker_callback(void* context) {
void nfc_read_mifare_desfire_worker_callback(NfcWorkerEvent event, void* context) {
Nfc* nfc = (Nfc*)context;
view_dispatcher_send_custom_event(nfc->view_dispatcher, NfcCustomEventWorkerExit);
}

View File

@ -1,13 +1,13 @@
#include "../nfc_i.h"
#include <dolphin/dolphin.h>
void nfc_read_mifare_ul_worker_callback(void* context) {
Nfc* nfc = (Nfc*)context;
void nfc_read_mifare_ul_worker_callback(NfcWorkerEvent event, void* context) {
Nfc* nfc = context;
view_dispatcher_send_custom_event(nfc->view_dispatcher, NfcCustomEventWorkerExit);
}
void nfc_scene_read_mifare_ul_on_enter(void* context) {
Nfc* nfc = (Nfc*)context;
Nfc* nfc = context;
DOLPHIN_DEED(DolphinDeedNfcRead);
// Setup view
@ -26,29 +26,25 @@ void nfc_scene_read_mifare_ul_on_enter(void* context) {
}
bool nfc_scene_read_mifare_ul_on_event(void* context, SceneManagerEvent event) {
Nfc* nfc = (Nfc*)context;
Nfc* nfc = context;
bool consumed = false;
if(event.type == SceneManagerEventTypeCustom) {
if(event.event == NfcCustomEventWorkerExit) {
scene_manager_next_scene(nfc->scene_manager, NfcSceneReadMifareUlSuccess);
return true;
consumed = true;
}
} else if(event.type == SceneManagerEventTypeTick) {
notification_message(nfc->notifications, &sequence_blink_blue_10);
return true;
consumed = true;
}
return false;
return consumed;
}
void nfc_scene_read_mifare_ul_on_exit(void* context) {
Nfc* nfc = (Nfc*)context;
Nfc* nfc = context;
// Stop worker
nfc_worker_stop(nfc->worker);
// Clear view
Popup* popup = nfc->popup;
popup_set_header(popup, NULL, 0, 0, AlignCenter, AlignBottom);
popup_set_text(popup, NULL, 0, 0, AlignCenter, AlignTop);
popup_set_icon(popup, 0, 0, NULL);
popup_reset(nfc->popup);
}

View File

@ -3,17 +3,17 @@
enum SubmenuIndex {
SubmenuIndexBankCard,
SubmenuIndexMifareUltralight,
SubmenuIdexReadMfClassic,
SubmenuIndexMifareDesfire,
};
void nfc_scene_scripts_menu_submenu_callback(void* context, uint32_t index) {
Nfc* nfc = (Nfc*)context;
Nfc* nfc = context;
view_dispatcher_send_custom_event(nfc->view_dispatcher, index);
}
void nfc_scene_scripts_menu_on_enter(void* context) {
Nfc* nfc = (Nfc*)context;
Nfc* nfc = context;
Submenu* submenu = nfc->submenu;
submenu_add_item(
@ -28,6 +28,12 @@ void nfc_scene_scripts_menu_on_enter(void* context) {
SubmenuIndexMifareUltralight,
nfc_scene_scripts_menu_submenu_callback,
nfc);
submenu_add_item(
submenu,
"Read Mifare Classic",
SubmenuIdexReadMfClassic,
nfc_scene_scripts_menu_submenu_callback,
nfc);
submenu_add_item(
submenu,
"Read Mifare DESFire",
@ -40,32 +46,37 @@ void nfc_scene_scripts_menu_on_enter(void* context) {
}
bool nfc_scene_scripts_menu_on_event(void* context, SceneManagerEvent event) {
Nfc* nfc = (Nfc*)context;
Nfc* nfc = context;
bool consumed = false;
if(event.type == SceneManagerEventTypeCustom) {
if(event.event == SubmenuIndexBankCard) {
scene_manager_set_scene_state(
nfc->scene_manager, NfcSceneScriptsMenu, SubmenuIndexBankCard);
scene_manager_next_scene(nfc->scene_manager, NfcSceneReadEmvApp);
return true;
consumed = true;
} else if(event.event == SubmenuIndexMifareUltralight) {
scene_manager_set_scene_state(
nfc->scene_manager, NfcSceneScriptsMenu, SubmenuIndexMifareUltralight);
scene_manager_next_scene(nfc->scene_manager, NfcSceneReadMifareUl);
return true;
consumed = true;
} else if(event.event == SubmenuIdexReadMfClassic) {
scene_manager_set_scene_state(
nfc->scene_manager, NfcSceneScriptsMenu, SubmenuIdexReadMfClassic);
scene_manager_next_scene(nfc->scene_manager, NfcSceneReadMifareClassic);
consumed = true;
} else if(event.event == SubmenuIndexMifareDesfire) {
scene_manager_set_scene_state(
nfc->scene_manager, NfcSceneScriptsMenu, SubmenuIndexMifareDesfire);
scene_manager_next_scene(nfc->scene_manager, NfcSceneReadMifareDesfire);
return true;
consumed = true;
}
}
return false;
return consumed;
}
void nfc_scene_scripts_menu_on_exit(void* context) {
Nfc* nfc = (Nfc*)context;
Nfc* nfc = context;
submenu_reset(nfc->submenu);
}

View File

@ -49,21 +49,15 @@ bool nfc_scene_start_on_event(void* context, SceneManagerEvent event) {
if(event.type == SceneManagerEventTypeCustom) {
if(event.event == SubmenuIndexRead) {
scene_manager_set_scene_state(nfc->scene_manager, NfcSceneStart, SubmenuIndexRead);
scene_manager_next_scene(nfc->scene_manager, NfcSceneReadCard);
consumed = true;
} else if(event.event == SubmenuIndexRunScript) {
scene_manager_set_scene_state(
nfc->scene_manager, NfcSceneStart, SubmenuIndexRunScript);
scene_manager_next_scene(nfc->scene_manager, NfcSceneScriptsMenu);
consumed = true;
} else if(event.event == SubmenuIndexSaved) {
scene_manager_set_scene_state(nfc->scene_manager, NfcSceneStart, SubmenuIndexSaved);
scene_manager_next_scene(nfc->scene_manager, NfcSceneFileSelect);
consumed = true;
} else if(event.event == SubmenuIndexAddManualy) {
scene_manager_set_scene_state(
nfc->scene_manager, NfcSceneStart, SubmenuIndexAddManualy);
scene_manager_next_scene(nfc->scene_manager, NfcSceneSetType);
consumed = true;
} else if(event.event == SubmenuIndexDebug) {
@ -71,6 +65,7 @@ bool nfc_scene_start_on_event(void* context, SceneManagerEvent event) {
scene_manager_next_scene(nfc->scene_manager, NfcSceneDebug);
consumed = true;
}
scene_manager_set_scene_state(nfc->scene_manager, NfcSceneStart, event.event);
}
return consumed;
}

View File

@ -0,0 +1,194 @@
#include "dict_attack.h"
#include <m-string.h>
#include <gui/elements.h>
typedef enum {
DictAttackStateSearchCard,
DictAttackStateSearchKeys,
DictAttackStateCardRemoved,
DictAttackStateSuccess,
DictAttackStateFail,
} DictAttackState;
struct DictAttack {
View* view;
DictAttackResultCallback callback;
void* context;
};
typedef struct {
DictAttackState state;
MfClassicType type;
uint8_t current_sector;
uint8_t total_sectors;
uint8_t keys_a_found;
uint8_t keys_a_total;
uint8_t keys_b_found;
uint8_t keys_b_total;
} DictAttackViewModel;
static void dict_attack_draw_callback(Canvas* canvas, void* model) {
DictAttackViewModel* m = model;
if(m->state == DictAttackStateSearchCard) {
canvas_set_font(canvas, FontPrimary);
canvas_draw_str_aligned(
canvas, 64, 32, AlignCenter, AlignCenter, "Detecting Mifare Classic");
} else if(m->state == DictAttackStateCardRemoved) {
canvas_set_font(canvas, FontPrimary);
canvas_draw_str_aligned(
canvas, 64, 32, AlignCenter, AlignTop, "Place card back to flipper");
} else {
char draw_str[32];
if(m->state == DictAttackStateSearchKeys) {
snprintf(
draw_str, sizeof(draw_str), "Searching keys for sector %d", m->current_sector);
canvas_draw_str_aligned(canvas, 64, 2, AlignCenter, AlignTop, draw_str);
} else if(m->state == DictAttackStateSuccess) {
canvas_draw_str_aligned(canvas, 64, 2, AlignCenter, AlignTop, "Complete!");
elements_button_right(canvas, "Save");
} else if(m->state == DictAttackStateFail) {
canvas_draw_str_aligned(
canvas, 64, 2, AlignCenter, AlignTop, "Failed to read any sector");
}
uint16_t keys_found = m->keys_a_found + m->keys_b_found;
uint16_t keys_total = m->keys_a_total + m->keys_b_total;
float progress = (float)(m->current_sector) / (float)(m->total_sectors);
elements_progress_bar(canvas, 5, 12, 120, progress);
canvas_set_font(canvas, FontSecondary);
snprintf(draw_str, sizeof(draw_str), "Total keys found: %d/%d", keys_found, keys_total);
canvas_draw_str_aligned(canvas, 1, 23, AlignLeft, AlignTop, draw_str);
snprintf(
draw_str, sizeof(draw_str), "A keys found: %d/%d", m->keys_a_found, m->keys_a_total);
canvas_draw_str_aligned(canvas, 1, 34, AlignLeft, AlignTop, draw_str);
snprintf(
draw_str, sizeof(draw_str), "B keys found: %d/%d", m->keys_b_found, m->keys_b_total);
canvas_draw_str_aligned(canvas, 1, 45, AlignLeft, AlignTop, draw_str);
}
}
static bool dict_attack_input_callback(InputEvent* event, void* context) {
DictAttack* dict_attack = context;
bool consumed = false;
DictAttackState state;
with_view_model(
dict_attack->view, (DictAttackViewModel * model) {
state = model->state;
return false;
});
if(state == DictAttackStateSuccess && event->type == InputTypeShort &&
event->key == InputKeyRight) {
if(dict_attack->callback) {
dict_attack->callback(dict_attack->context);
}
consumed = true;
}
return consumed;
}
DictAttack* dict_attack_alloc() {
DictAttack* dict_attack = malloc(sizeof(DictAttack));
dict_attack->view = view_alloc();
view_allocate_model(dict_attack->view, ViewModelTypeLocking, sizeof(DictAttackViewModel));
view_set_draw_callback(dict_attack->view, dict_attack_draw_callback);
view_set_input_callback(dict_attack->view, dict_attack_input_callback);
view_set_context(dict_attack->view, dict_attack);
return dict_attack;
}
void dict_attack_free(DictAttack* dict_attack) {
furi_assert(dict_attack);
view_free(dict_attack->view);
free(dict_attack);
}
void dict_attack_reset(DictAttack* dict_attack) {
furi_assert(dict_attack);
with_view_model(
dict_attack->view, (DictAttackViewModel * model) {
memset(model, 0, sizeof(DictAttackViewModel));
return true;
});
}
View* dict_attack_get_view(DictAttack* dict_attack) {
furi_assert(dict_attack);
return dict_attack->view;
}
void dict_attack_set_result_callback(
DictAttack* dict_attack,
DictAttackResultCallback callback,
void* context) {
furi_assert(dict_attack);
furi_assert(callback);
dict_attack->callback = callback;
dict_attack->context = context;
}
void dict_attack_card_detected(DictAttack* dict_attack, MfClassicType type) {
furi_assert(dict_attack);
with_view_model(
dict_attack->view, (DictAttackViewModel * model) {
model->state = DictAttackStateSearchKeys;
if(type == MfClassicType1k) {
model->total_sectors = 16;
model->keys_a_total = 16;
model->keys_b_total = 16;
} else if(type == MfClassicType4k) {
model->total_sectors = 40;
model->keys_a_total = 40;
model->keys_b_total = 40;
}
return true;
});
}
void dict_attack_card_removed(DictAttack* dict_attack) {
furi_assert(dict_attack);
with_view_model(
dict_attack->view, (DictAttackViewModel * model) {
if(model->state == DictAttackStateSearchKeys) {
model->state = DictAttackStateCardRemoved;
} else {
model->state = DictAttackStateSearchCard;
}
return true;
});
}
void dict_attack_inc_curr_sector(DictAttack* dict_attack) {
furi_assert(dict_attack);
with_view_model(
dict_attack->view, (DictAttackViewModel * model) {
model->current_sector++;
return true;
});
}
void dict_attack_inc_found_key(DictAttack* dict_attack, MfClassicKey key) {
furi_assert(dict_attack);
with_view_model(
dict_attack->view, (DictAttackViewModel * model) {
model->state = DictAttackStateSearchKeys;
if(key == MfClassicKeyA) {
model->keys_a_found++;
} else if(key == MfClassicKeyB) {
model->keys_b_found++;
}
return true;
});
}
void dict_attack_set_result(DictAttack* dict_attack, bool success) {
furi_assert(dict_attack);
with_view_model(
dict_attack->view, (DictAttackViewModel * model) {
if(success) {
model->state = DictAttackStateSuccess;
} else {
model->state = DictAttackStateFail;
}
return true;
});
}

View File

@ -0,0 +1,33 @@
#pragma once
#include <stdint.h>
#include <gui/view.h>
#include <gui/modules/widget.h>
#include <lib/nfc_protocols/mifare_classic.h>
typedef struct DictAttack DictAttack;
typedef void (*DictAttackResultCallback)(void* context);
DictAttack* dict_attack_alloc();
void dict_attack_free(DictAttack* dict_attack);
void dict_attack_reset(DictAttack* dict_attack);
View* dict_attack_get_view(DictAttack* dict_attack);
void dict_attack_set_result_callback(
DictAttack* dict_attack,
DictAttackResultCallback callback,
void* context);
void dict_attack_card_detected(DictAttack* dict_attack, MfClassicType type);
void dict_attack_card_removed(DictAttack* dict_attack);
void dict_attack_inc_curr_sector(DictAttack* dict_attack);
void dict_attack_inc_found_key(DictAttack* dict_attack, MfClassicKey key);
void dict_attack_set_result(DictAttack* dict_attack, bool success);

File diff suppressed because it is too large Load Diff

5
core/furi/common_defines.h Normal file → Executable file
View File

@ -1,7 +1,6 @@
#pragma once
#ifndef MAX
#define MAX(a, b) \
({ \
__typeof__(a) _a = (a); \
@ -72,6 +71,10 @@
(((x)&0xFF000000) >> 24))
#endif
#ifndef FURI_BIT
#define FURI_BIT(x, n) ((x) >> (n)&1)
#endif
#ifndef FURI_CRITICAL_ENTER
#define FURI_CRITICAL_ENTER() \
uint32_t primask_bit = __get_PRIMASK(); \

View File

@ -103,6 +103,61 @@ bool furi_hal_nfc_detect(
return true;
}
bool furi_hal_nfc_activate_nfca(uint32_t timeout, uint32_t* cuid) {
rfalNfcDevice* dev_list;
uint8_t dev_cnt = 0;
rfalLowPowerModeStop();
rfalNfcState state = rfalNfcGetState();
if(state == RFAL_NFC_STATE_NOTINIT) {
rfalNfcInitialize();
}
rfalNfcDiscoverParam params = {
.compMode = RFAL_COMPLIANCE_MODE_NFC,
.techs2Find = RFAL_NFC_POLL_TECH_A,
.totalDuration = 1000,
.devLimit = 3,
.wakeupEnabled = false,
.wakeupConfigDefault = true,
.nfcfBR = RFAL_BR_212,
.ap2pBR = RFAL_BR_424,
.maxBR = RFAL_BR_KEEP,
.GBLen = RFAL_NFCDEP_GB_MAX_LEN,
.notifyCb = NULL,
};
uint32_t start = DWT->CYCCNT;
rfalNfcDiscover(&params);
while(state != RFAL_NFC_STATE_ACTIVATED) {
rfalNfcWorker();
state = rfalNfcGetState();
FURI_LOG_T(TAG, "Current state %d", state);
if(state == RFAL_NFC_STATE_POLL_ACTIVATION) {
start = DWT->CYCCNT;
continue;
}
if(state == RFAL_NFC_STATE_POLL_SELECT) {
rfalNfcSelect(0);
}
if(DWT->CYCCNT - start > timeout * clocks_in_ms) {
rfalNfcDeactivate(true);
FURI_LOG_T(TAG, "Timeout");
return false;
}
osThreadYield();
}
rfalNfcGetDevicesFound(&dev_list, &dev_cnt);
// Take first device and set cuid
if(cuid) {
uint8_t* cuid_start = dev_list[0].nfcid;
if(dev_list[0].nfcidLen == 7) {
cuid_start = &dev_list[0].nfcid[3];
}
*cuid = (cuid_start[0] << 24) | (cuid_start[1] << 16) | (cuid_start[2] << 8) |
(cuid_start[3]);
FURI_LOG_T(TAG, "Activated tag with cuid: %lX", *cuid);
}
return true;
}
bool furi_hal_nfc_listen(
uint8_t* uid,
uint8_t uid_len,
@ -297,12 +352,10 @@ ReturnCode furi_hal_nfc_data_exchange(
rfalNfcWorker();
state = rfalNfcGetState();
ret = rfalNfcDataExchangeGetStatus();
if(ret > ERR_SLEEP_REQ) {
return ret;
}
if(ret == ERR_BUSY) {
if(DWT->CYCCNT - start > 1000 * clocks_in_ms) {
return ERR_TIMEOUT;
ret = ERR_TIMEOUT;
break;
}
continue;
} else {
@ -314,36 +367,100 @@ ReturnCode furi_hal_nfc_data_exchange(
rfalNfcDeactivate(false);
rfalLowPowerModeStart();
}
return ERR_NONE;
return ret;
}
ReturnCode furi_hal_nfc_raw_bitstream_exchange(
uint8_t* tx_buff,
uint16_t tx_bit_len,
uint8_t** rx_buff,
uint16_t** rx_bit_len,
bool deactivate) {
furi_assert(rx_buff);
furi_assert(rx_bit_len);
static uint16_t furi_hal_nfc_data_and_parity_to_bitstream(
uint8_t* data,
uint16_t len,
uint8_t* parity,
uint8_t* out) {
furi_assert(data);
furi_assert(out);
uint8_t next_par_bit = 0;
uint16_t curr_bit_pos = 0;
for(uint16_t i = 0; i < len; i++) {
next_par_bit = FURI_BIT(parity[i / 8], 7 - (i % 8));
if(curr_bit_pos % 8 == 0) {
out[curr_bit_pos / 8] = data[i];
curr_bit_pos += 8;
out[curr_bit_pos / 8] = next_par_bit;
curr_bit_pos++;
} else {
out[curr_bit_pos / 8] |= data[i] << curr_bit_pos % 8;
out[curr_bit_pos / 8 + 1] = data[i] >> (8 - curr_bit_pos % 8);
out[curr_bit_pos / 8 + 1] |= next_par_bit << curr_bit_pos % 8;
curr_bit_pos += 9;
}
}
return curr_bit_pos;
}
uint16_t furi_hal_nfc_bitstream_to_data_and_parity(
uint8_t* in_buff,
uint16_t in_buff_bits,
uint8_t* out_data,
uint8_t* out_parity) {
if(in_buff_bits % 9 != 0) {
return 0;
}
uint8_t curr_byte = 0;
uint16_t bit_processed = 0;
memset(out_parity, 0, in_buff_bits / 9);
while(bit_processed < in_buff_bits) {
out_data[curr_byte] = in_buff[bit_processed / 8] >> bit_processed % 8;
out_data[curr_byte] |= in_buff[bit_processed / 8 + 1] << (8 - bit_processed % 8);
out_parity[curr_byte / 8] |= FURI_BIT(in_buff[bit_processed / 8 + 1], bit_processed % 8)
<< (7 - curr_byte % 8);
bit_processed += 9;
curr_byte++;
}
return curr_byte;
}
bool furi_hal_nfc_tx_rx(FuriHalNfcTxRxContext* tx_rx_ctx) {
furi_assert(tx_rx_ctx);
ReturnCode ret;
rfalNfcState state = RFAL_NFC_STATE_ACTIVATED;
ret =
rfalNfcDataExchangeStart(tx_buff, tx_bit_len, rx_buff, rx_bit_len, 0, RFAL_TXRX_FLAGS_RAW);
uint8_t temp_tx_buff[FURI_HAL_NFC_DATA_BUFF_SIZE] = {};
uint16_t temp_tx_bits = 0;
uint8_t* temp_rx_buff = NULL;
uint16_t* temp_rx_bits = NULL;
// Prepare data for FIFO if necessary
if(tx_rx_ctx->tx_rx_type == FURI_HAL_NFC_TXRX_RAW) {
temp_tx_bits = furi_hal_nfc_data_and_parity_to_bitstream(
tx_rx_ctx->tx_data, tx_rx_ctx->tx_bits / 8, tx_rx_ctx->tx_parity, temp_tx_buff);
ret = rfalNfcDataExchangeCustomStart(
temp_tx_buff,
temp_tx_bits,
&temp_rx_buff,
&temp_rx_bits,
RFAL_FWT_NONE,
tx_rx_ctx->tx_rx_type);
} else {
ret = rfalNfcDataExchangeCustomStart(
tx_rx_ctx->tx_data,
tx_rx_ctx->tx_bits,
&temp_rx_buff,
&temp_rx_bits,
RFAL_FWT_NONE,
tx_rx_ctx->tx_rx_type);
}
if(ret != ERR_NONE) {
return ret;
return false;
}
uint32_t start = DWT->CYCCNT;
while(state != RFAL_NFC_STATE_DATAEXCHANGE_DONE) {
rfalNfcWorker();
state = rfalNfcGetState();
ret = rfalNfcDataExchangeGetStatus();
if(ret > ERR_SLEEP_REQ) {
return ret;
}
if(ret == ERR_BUSY) {
if(DWT->CYCCNT - start > 1000 * clocks_in_ms) {
return ERR_TIMEOUT;
if(DWT->CYCCNT - start > 4 * clocks_in_ms) {
return false;
}
continue;
} else {
@ -351,11 +468,16 @@ ReturnCode furi_hal_nfc_raw_bitstream_exchange(
}
taskYIELD();
}
if(deactivate) {
rfalNfcDeactivate(false);
rfalLowPowerModeStart();
if(tx_rx_ctx->tx_rx_type == FURI_HAL_NFC_TXRX_RAW) {
tx_rx_ctx->rx_bits =
8 * furi_hal_nfc_bitstream_to_data_and_parity(
temp_rx_buff, *temp_rx_bits, tx_rx_ctx->rx_data, tx_rx_ctx->rx_parity);
} else {
memcpy(tx_rx_ctx->rx_data, temp_rx_buff, *temp_rx_bits / 8);
}
return ERR_NONE;
return true;
}
void furi_hal_nfc_deactivate() {

52
firmware/targets/furi_hal_include/furi_hal_nfc.h Normal file → Executable file
View File

@ -15,6 +15,8 @@ extern "C" {
#endif
#define FURI_HAL_NFC_UID_MAX_LEN 10
#define FURI_HAL_NFC_DATA_BUFF_SIZE (64)
#define FURI_HAL_NFC_PARITY_BUFF_SIZE (FURI_HAL_NFC_DATA_BUFF_SIZE / 8)
#define FURI_HAL_NFC_TXRX_DEFAULT \
((uint32_t)RFAL_TXRX_FLAGS_CRC_TX_AUTO | (uint32_t)RFAL_TXRX_FLAGS_CRC_RX_REMV | \
@ -22,10 +24,22 @@ extern "C" {
(uint32_t)RFAL_TXRX_FLAGS_PAR_RX_REMV | (uint32_t)RFAL_TXRX_FLAGS_PAR_TX_AUTO | \
(uint32_t)RFAL_TXRX_FLAGS_NFCV_FLAG_AUTO)
#define FURI_HAL_NFC_TXRX_RAW \
((uint32_t)RFAL_TXRX_FLAGS_CRC_TX_MANUAL | (uint32_t)RFAL_TXRX_FLAGS_CRC_RX_REMV | \
#define FURI_HAL_NFC_TX_DEFAULT_RX_NO_CRC \
((uint32_t)RFAL_TXRX_FLAGS_CRC_TX_AUTO | (uint32_t)RFAL_TXRX_FLAGS_CRC_RX_KEEP | \
(uint32_t)RFAL_TXRX_FLAGS_NFCIP1_OFF | (uint32_t)RFAL_TXRX_FLAGS_AGC_ON | \
(uint32_t)RFAL_TXRX_FLAGS_PAR_RX_REMV | (uint32_t)RFAL_TXRX_FLAGS_PAR_TX_AUTO | \
(uint32_t)RFAL_TXRX_FLAGS_NFCV_FLAG_AUTO)
#define FURI_HAL_NFC_TXRX_WITH_PAR \
((uint32_t)RFAL_TXRX_FLAGS_CRC_TX_MANUAL | (uint32_t)RFAL_TXRX_FLAGS_CRC_RX_KEEP | \
(uint32_t)RFAL_TXRX_FLAGS_NFCIP1_OFF | (uint32_t)RFAL_TXRX_FLAGS_AGC_ON | \
(uint32_t)RFAL_TXRX_FLAGS_PAR_RX_REMV | (uint32_t)RFAL_TXRX_FLAGS_PAR_TX_NONE | \
(uint32_t)RFAL_TXRX_FLAGS_PAR_RX_KEEP | (uint32_t)RFAL_TXRX_FLAGS_PAR_TX_AUTO | \
(uint32_t)RFAL_TXRX_FLAGS_NFCV_FLAG_AUTO)
#define FURI_HAL_NFC_TXRX_RAW \
((uint32_t)RFAL_TXRX_FLAGS_CRC_TX_MANUAL | (uint32_t)RFAL_TXRX_FLAGS_CRC_RX_KEEP | \
(uint32_t)RFAL_TXRX_FLAGS_NFCIP1_OFF | (uint32_t)RFAL_TXRX_FLAGS_AGC_ON | \
(uint32_t)RFAL_TXRX_FLAGS_PAR_RX_KEEP | (uint32_t)RFAL_TXRX_FLAGS_PAR_TX_NONE | \
(uint32_t)RFAL_TXRX_FLAGS_NFCV_FLAG_AUTO)
typedef bool (*FuriHalNfcEmulateCallback)(
@ -36,6 +50,16 @@ typedef bool (*FuriHalNfcEmulateCallback)(
uint32_t* flags,
void* context);
typedef struct {
uint8_t tx_data[FURI_HAL_NFC_DATA_BUFF_SIZE];
uint8_t tx_parity[FURI_HAL_NFC_PARITY_BUFF_SIZE];
uint16_t tx_bits;
uint8_t rx_data[FURI_HAL_NFC_DATA_BUFF_SIZE];
uint8_t rx_parity[FURI_HAL_NFC_PARITY_BUFF_SIZE];
uint16_t rx_bits;
uint32_t tx_rx_type;
} FuriHalNfcTxRxContext;
/** Init nfc
*/
void furi_hal_nfc_init();
@ -77,6 +101,15 @@ bool furi_hal_nfc_detect(
uint32_t timeout,
bool deactivate);
/** Activate NFC-A tag
*
* @param timeout timeout in ms
* @param cuid pointer to 32bit uid
*
* @return true on succeess
*/
bool furi_hal_nfc_activate_nfca(uint32_t timeout, uint32_t* cuid);
/** NFC listen
*
* @param uid pointer to uid buffer
@ -131,12 +164,13 @@ ReturnCode furi_hal_nfc_data_exchange(
uint16_t** rx_len,
bool deactivate);
ReturnCode furi_hal_nfc_raw_bitstream_exchange(
uint8_t* tx_buff,
uint16_t tx_bit_len,
uint8_t** rx_buff,
uint16_t** rx_bit_len,
bool deactivate);
/** NFC data exchange
*
* @param tx_rx_ctx FuriHalNfcTxRxContext instance
*
* @return true on success
*/
bool furi_hal_nfc_tx_rx(FuriHalNfcTxRxContext* tx_rx_ctx);
/** NFC deactivate and start sleep
*/

View File

@ -367,6 +367,14 @@ ReturnCode rfalNfcDataExchangeStart(
uint32_t fwt,
uint32_t tx_flag);
ReturnCode rfalNfcDataExchangeCustomStart(
uint8_t* txData,
uint16_t txDataLen,
uint8_t** rxData,
uint16_t** rvdLen,
uint32_t fwt,
uint32_t flags);
/*!
*****************************************************************************
* \brief RFAL NFC Get Data Exchange Status

View File

@ -115,9 +115,9 @@
(uint32_t)RFAL_TXRX_FLAGS_PAR_RX_REMV | (uint32_t)RFAL_TXRX_FLAGS_PAR_TX_AUTO | \
(uint32_t)RFAL_TXRX_FLAGS_NFCV_FLAG_AUTO)
#define RFAL_TXRX_FLAGS_RAW \
((uint32_t)RFAL_TXRX_FLAGS_CRC_TX_MANUAL | (uint32_t)RFAL_TXRX_FLAGS_CRC_RX_REMV | \
((uint32_t)RFAL_TXRX_FLAGS_CRC_TX_MANUAL | (uint32_t)RFAL_TXRX_FLAGS_CRC_RX_KEEP | \
(uint32_t)RFAL_TXRX_FLAGS_NFCIP1_OFF | (uint32_t)RFAL_TXRX_FLAGS_AGC_ON | \
(uint32_t)RFAL_TXRX_FLAGS_PAR_RX_REMV | (uint32_t)RFAL_TXRX_FLAGS_PAR_TX_NONE | \
(uint32_t)RFAL_TXRX_FLAGS_PAR_RX_KEEP | (uint32_t)RFAL_TXRX_FLAGS_PAR_TX_NONE | \
(uint32_t)RFAL_TXRX_FLAGS_NFCV_FLAG_AUTO)
#define RFAL_LM_MASK_NFCA \

134
lib/ST25RFAL002/source/rfal_nfc.c Normal file → Executable file
View File

@ -686,6 +686,140 @@ ReturnCode rfalNfcDataExchangeStart(
return ERR_WRONG_STATE;
}
ReturnCode rfalNfcDataExchangeCustomStart(
uint8_t* txData,
uint16_t txDataLen,
uint8_t** rxData,
uint16_t** rvdLen,
uint32_t fwt,
uint32_t flags) {
ReturnCode err;
rfalTransceiveContext ctx;
/*******************************************************************************/
/* The Data Exchange is divided in two different moments, the trigger/Start of *
* the transfer followed by the check until its completion */
if((gNfcDev.state >= RFAL_NFC_STATE_ACTIVATED) && (gNfcDev.activeDev != NULL)) {
/*******************************************************************************/
/* In Listen mode is the Poller that initiates the communicatation */
/* Assign output parameters and rfalNfcDataExchangeGetStatus will return */
/* incoming data from Poller/Initiator */
if((gNfcDev.state == RFAL_NFC_STATE_ACTIVATED) &&
rfalNfcIsRemDevPoller(gNfcDev.activeDev->type)) {
if(txDataLen > 0U) {
return ERR_WRONG_STATE;
}
*rvdLen = (uint16_t*)&gNfcDev.rxLen;
*rxData = (uint8_t*)( (gNfcDev.activeDev->rfInterface == RFAL_NFC_INTERFACE_ISODEP) ? gNfcDev.rxBuf.isoDepBuf.apdu :
((gNfcDev.activeDev->rfInterface == RFAL_NFC_INTERFACE_NFCDEP) ? gNfcDev.rxBuf.nfcDepBuf.pdu : gNfcDev.rxBuf.rfBuf));
if(gNfcDev.disc.activate_after_sak) {
gNfcDev.state = RFAL_NFC_STATE_DATAEXCHANGE_DONE;
}
return ERR_NONE;
}
/*******************************************************************************/
switch(gNfcDev.activeDev
->rfInterface) /* Check which RF interface shall be used/has been activated */
{
/*******************************************************************************/
case RFAL_NFC_INTERFACE_RF:
ctx.rxBuf = gNfcDev.rxBuf.rfBuf, ctx.rxBufLen = sizeof(gNfcDev.rxBuf.rfBuf),
ctx.rxRcvdLen = &gNfcDev.rxLen, ctx.txBuf = txData, ctx.txBufLen = txDataLen,
ctx.flags = flags, ctx.fwt = fwt, *rxData = (uint8_t*)gNfcDev.rxBuf.rfBuf;
*rvdLen = (uint16_t*)&gNfcDev.rxLen;
err = rfalStartTransceive(&ctx);
break;
#if RFAL_FEATURE_ISO_DEP
/*******************************************************************************/
case RFAL_NFC_INTERFACE_ISODEP: {
rfalIsoDepApduTxRxParam isoDepTxRx;
if(txDataLen > sizeof(gNfcDev.txBuf.isoDepBuf.apdu)) {
return ERR_NOMEM;
}
if(txDataLen > 0U) {
ST_MEMCPY((uint8_t*)gNfcDev.txBuf.isoDepBuf.apdu, txData, txDataLen);
}
isoDepTxRx.DID = RFAL_ISODEP_NO_DID;
isoDepTxRx.ourFSx = RFAL_ISODEP_FSX_KEEP;
isoDepTxRx.FSx = gNfcDev.activeDev->proto.isoDep.info.FSx;
isoDepTxRx.dFWT = gNfcDev.activeDev->proto.isoDep.info.dFWT;
isoDepTxRx.FWT = gNfcDev.activeDev->proto.isoDep.info.FWT;
isoDepTxRx.txBuf = &gNfcDev.txBuf.isoDepBuf;
isoDepTxRx.txBufLen = txDataLen;
isoDepTxRx.rxBuf = &gNfcDev.rxBuf.isoDepBuf;
isoDepTxRx.rxLen = &gNfcDev.rxLen;
isoDepTxRx.tmpBuf = &gNfcDev.tmpBuf.isoDepBuf;
*rxData = (uint8_t*)gNfcDev.rxBuf.isoDepBuf.apdu;
*rvdLen = (uint16_t*)&gNfcDev.rxLen;
/*******************************************************************************/
/* Trigger a RFAL ISO-DEP Transceive */
err = rfalIsoDepStartApduTransceive(isoDepTxRx);
break;
}
#endif /* RFAL_FEATURE_ISO_DEP */
#if RFAL_FEATURE_NFC_DEP
/*******************************************************************************/
case RFAL_NFC_INTERFACE_NFCDEP: {
rfalNfcDepPduTxRxParam nfcDepTxRx;
if(txDataLen > sizeof(gNfcDev.txBuf.nfcDepBuf.pdu)) {
return ERR_NOMEM;
}
if(txDataLen > 0U) {
ST_MEMCPY((uint8_t*)gNfcDev.txBuf.nfcDepBuf.pdu, txData, txDataLen);
}
nfcDepTxRx.DID = RFAL_NFCDEP_DID_KEEP;
nfcDepTxRx.FSx =
rfalNfcIsRemDevListener(gNfcDev.activeDev->type) ?
rfalNfcDepLR2FS((uint8_t)rfalNfcDepPP2LR(
gNfcDev.activeDev->proto.nfcDep.activation.Target.ATR_RES.PPt)) :
rfalNfcDepLR2FS((uint8_t)rfalNfcDepPP2LR(
gNfcDev.activeDev->proto.nfcDep.activation.Initiator.ATR_REQ.PPi));
nfcDepTxRx.dFWT = gNfcDev.activeDev->proto.nfcDep.info.dFWT;
nfcDepTxRx.FWT = gNfcDev.activeDev->proto.nfcDep.info.FWT;
nfcDepTxRx.txBuf = &gNfcDev.txBuf.nfcDepBuf;
nfcDepTxRx.txBufLen = txDataLen;
nfcDepTxRx.rxBuf = &gNfcDev.rxBuf.nfcDepBuf;
nfcDepTxRx.rxLen = &gNfcDev.rxLen;
nfcDepTxRx.tmpBuf = &gNfcDev.tmpBuf.nfcDepBuf;
*rxData = (uint8_t*)gNfcDev.rxBuf.nfcDepBuf.pdu;
*rvdLen = (uint16_t*)&gNfcDev.rxLen;
/*******************************************************************************/
/* Trigger a RFAL NFC-DEP Transceive */
err = rfalNfcDepStartPduTransceive(nfcDepTxRx);
break;
}
#endif /* RFAL_FEATURE_NFC_DEP */
/*******************************************************************************/
default:
err = ERR_PARAM;
break;
}
/* If a transceive has succesfully started flag Data Exchange as ongoing */
if(err == ERR_NONE) {
gNfcDev.dataExErr = ERR_BUSY;
gNfcDev.state = RFAL_NFC_STATE_DATAEXCHANGE;
}
return err;
}
return ERR_WRONG_STATE;
}
/*******************************************************************************/
ReturnCode rfalNfcDataExchangeGetStatus(void) {
/*******************************************************************************/

View 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;
}
uint8_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);
}

View 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);
uint8_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);

View File

@ -0,0 +1,314 @@
#include "mifare_classic.h"
#include "nfca.h"
#include "nfc_util.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)
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;
}
}
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_total_sectors_num(MfClassicReader* reader) {
furi_assert(reader);
if(reader->type == MfClassicType1k) {
return MF_CLASSIC_1K_TOTAL_SECTORS_NUM;
} else if(reader->type == MfClassicType4k) {
return MF_CLASSIC_4K_TOTAL_SECTORS_NUM;
} else {
return 0;
}
}
bool mf_classic_check_card_type(uint8_t ATQA0, uint8_t ATQA1, uint8_t SAK) {
if((ATQA0 == 0x44 || ATQA0 == 0x04) && (SAK == 0x08)) {
return true;
} else if((ATQA0 == 0x42 || ATQA0 == 0x02) && (SAK == 0x18)) {
return true;
} else {
return false;
}
}
bool mf_classic_get_type(
uint8_t* uid,
uint8_t uid_len,
uint8_t ATQA0,
uint8_t ATQA1,
uint8_t SAK,
MfClassicReader* reader) {
furi_assert(uid);
furi_assert(reader);
memset(reader, 0, sizeof(MfClassicReader));
if((ATQA0 == 0x44 || ATQA0 == 0x04) && (SAK == 0x08)) {
reader->type = MfClassicType1k;
} else if((ATQA0 == 0x42 || ATQA0 == 0x02) && (SAK == 0x18)) {
reader->type = MfClassicType4k;
} else {
return false;
}
uint8_t* cuid_start = uid;
if(uid_len == 7) {
cuid_start = &uid[3];
}
reader->cuid = (cuid_start[0] << 24) | (cuid_start[1] << 16) | (cuid_start[2] << 8) |
(cuid_start[3]);
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 - 1) {
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, uint32_t cuid, uint8_t sector) {
furi_assert(auth_ctx);
auth_ctx->cuid = cuid;
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 cuid,
uint32_t block,
uint64_t key,
MfClassicKey key_type,
Crypto1* crypto) {
bool auth_success = false;
memset(tx_rx, 0, sizeof(FuriHalNfcTxRxContext));
do {
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 = FURI_HAL_NFC_TX_DEFAULT_RX_NO_CRC;
tx_rx->tx_bits = 2 * 8;
if(!furi_hal_nfc_tx_rx(tx_rx)) 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] = {};
// uint8_t parity = 0;
nfc_util_num2bytes(prng_successor(DWT->CYCCNT, 32), 4, nr);
// uint8_t nr_ar[8] = {};
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 = FURI_HAL_NFC_TXRX_RAW;
tx_rx->tx_bits = 8 * 8;
if(!furi_hal_nfc_tx_rx(tx_rx)) break;
if(tx_rx->rx_bits == 32) {
crypto1_word(crypto, 0, 0);
auth_success = true;
}
} while(false);
return auth_success;
}
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,
auth_ctx->cuid,
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_deactivate();
furi_hal_nfc_activate_nfca(300, &auth_ctx->cuid);
}
if(auth_ctx->key_b == MF_CLASSIC_NO_KEY) {
// Try AUTH with key B
if(mf_classic_auth(
tx_rx,
auth_ctx->cuid,
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_num < MF_CLASSIC_TOTAL_BLOCKS_MAX);
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, 0, sizeof(FuriHalNfcTxRxContext));
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 = FURI_HAL_NFC_TXRX_RAW;
if(furi_hal_nfc_tx_rx(tx_rx)) {
if(tx_rx->rx_bits == 8 * 18) {
for(uint8_t i = 0; i < 18; i++) {
block->value[i] = crypto1_byte(crypto, 0, 0) ^ tx_rx->rx_data[i];
}
read_block_success = true;
}
}
return read_block_success;
}
bool mf_classic_read_sector(
FuriHalNfcTxRxContext* tx_rx,
Crypto1* crypto,
MfClassicSectorReader* sector_reader,
MfClassicSector* sector) {
furi_assert(tx_rx);
furi_assert(sector_reader);
furi_assert(sector);
uint32_t cuid = 0;
uint64_t key;
MfClassicKey key_type;
uint8_t first_block;
bool sector_read = false;
furi_hal_nfc_deactivate();
do {
// Activate card
if(!furi_hal_nfc_activate_nfca(200, &cuid)) break;
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, cuid, 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, &sector->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, &sector->block[sector->total_blocks - 1].value[0]);
}
if(sector_reader->key_b != MF_CLASSIC_NO_KEY) {
nfc_util_num2bytes(
sector_reader->key_b, 6, &sector->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;
MfClassicSector temp_sector = {};
for(uint8_t i = 0; i < reader->sectors_to_read; i++) {
if(mf_classic_read_sector(
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++) {
data->block[first_block + j] = temp_sector.block[j];
}
sectors_read++;
}
}
return sectors_read;
}

View File

@ -0,0 +1,102 @@
#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)
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[6];
uint8_t access_bits[4];
uint8_t key_b[6];
} MfClassicSectorTrailer;
typedef struct {
uint8_t total_blocks;
MfClassicBlock block[MF_CLASSIC_BLOCKS_IN_SECTOR_MAX];
} MfClassicSector;
typedef struct {
MfClassicType type;
MfClassicBlock block[MF_CLASSIC_TOTAL_BLOCKS_MAX];
} MfClassicData;
typedef struct {
uint32_t cuid;
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;
uint32_t cuid;
uint8_t sectors_to_read;
Crypto1 crypto;
MfClassicSectorReader sector_reader[MF_CLASSIC_SECTORS_MAX];
} MfClassicReader;
bool mf_classic_check_card_type(uint8_t ATQA0, uint8_t ATQA1, uint8_t SAK);
bool mf_classic_get_type(
uint8_t* uid,
uint8_t uid_len,
uint8_t ATQA0,
uint8_t ATQA1,
uint8_t SAK,
MfClassicReader* reader);
uint8_t mf_classic_get_total_sectors_num(MfClassicReader* reader);
void mf_classic_auth_init_context(MfClassicAuthContext* auth_ctx, uint32_t cuid, uint8_t sector);
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);
bool mf_classic_read_sector(
FuriHalNfcTxRxContext* tx_rx,
Crypto1* crypto,
MfClassicSectorReader* sector_reader,
MfClassicSector* sector);
uint8_t mf_classic_read_card(
FuriHalNfcTxRxContext* tx_rx,
MfClassicReader* reader,
MfClassicData* data);

View File

@ -0,0 +1,63 @@
#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];
}
void nfc_util_merge_data_and_parity(
uint8_t* data,
uint16_t data_len,
uint8_t* parity,
uint16_t parity_len,
uint8_t* res,
uint16_t* res_len);
void nfc_util_split_data_and_parity(
uint8_t* data,
uint16_t data_len,
uint8_t* parity,
uint16_t parity_len,
uint8_t* res,
uint16_t* res_len);

View File

@ -0,0 +1,27 @@
#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);
void nfc_util_merge_data_and_parity(
uint8_t* data,
uint16_t data_len,
uint8_t* parity,
uint16_t parity_len,
uint8_t* res,
uint16_t* res_len);
void nfc_util_split_data_and_parity(
uint8_t* data,
uint16_t data_len,
uint8_t* parity,
uint16_t parity_len,
uint8_t* res,
uint16_t* res_len);

View File

@ -4,6 +4,8 @@
#define NFCA_CMD_RATS (0xE0U)
#define NFCA_CRC_INIT (0x6363)
typedef struct {
uint8_t cmd;
uint8_t param;
@ -13,6 +15,27 @@ 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,

View File

@ -3,6 +3,10 @@
#include <stdint.h>
#include <stdbool.h>
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,

View File

@ -146,8 +146,7 @@ static int32_t subghz_file_encoder_worker_thread(void* context) {
size_t stream_free_byte = xStreamBufferSpacesAvailable(instance->stream);
if((stream_free_byte / sizeof(int32_t)) >= SUBGHZ_FILE_ENCODER_LOAD) {
if(stream_read_line(stream, instance->str_data)) {
//skip the end of the previous line "\n"
stream_seek(stream, 1, StreamOffsetFromCurrent);
string_strim(instance->str_data);
if(!subghz_file_encoder_worker_data_parse(
instance,
string_get_cstr(instance->str_data),

View File

@ -74,18 +74,17 @@ bool stream_read_line(Stream* stream, string_t str_result) {
do {
uint16_t bytes_were_read = stream_read(stream, buffer, buffer_size);
// TODO process EOF
if(bytes_were_read == 0) break;
bool result = false;
bool error = false;
for(uint16_t i = 0; i < bytes_were_read; i++) {
if(buffer[i] == '\n') {
if(!stream_seek(stream, i - bytes_were_read, StreamOffsetFromCurrent)) {
if(!stream_seek(stream, i - bytes_were_read + 1, StreamOffsetFromCurrent)) {
error = true;
break;
}
string_push_back(str_result, buffer[i]);
result = true;
break;
} else if(buffer[i] == '\r') {
@ -100,7 +99,7 @@ bool stream_read_line(Stream* stream, string_t str_result) {
}
} while(true);
return string_size(str_result) != 0;
return stream_eof(stream);
}
bool stream_rewind(Stream* stream) {

View File

@ -102,8 +102,8 @@ bool stream_delete_and_insert(
* Read line from a stream (supports LF and CRLF line endings)
* @param stream
* @param str_result
* @return true
* @return false
* @return true if line was read
* @return false if EOF
*/
bool stream_read_line(Stream* stream, string_t str_result);