[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:
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;
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
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);
|
||||
|
||||
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);
|
314
lib/nfc_protocols/mifare_classic.c
Normal file
314
lib/nfc_protocols/mifare_classic.c
Normal 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, §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;
|
||||
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;
|
||||
}
|
102
lib/nfc_protocols/mifare_classic.h
Normal file
102
lib/nfc_protocols/mifare_classic.h
Normal 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);
|
63
lib/nfc_protocols/nfc_util.c
Normal file
63
lib/nfc_protocols/nfc_util.c
Normal 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);
|
27
lib/nfc_protocols/nfc_util.h
Normal file
27
lib/nfc_protocols/nfc_util.h
Normal 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);
|
@@ -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,
|
||||
|
@@ -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,
|
||||
|
Reference in New Issue
Block a user