2022-03-23 22:14:34 +00:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <furi_hal_nfc.h>
|
|
|
|
|
|
|
|
#include "crypto1.h"
|
|
|
|
|
|
|
|
#define MF_CLASSIC_BLOCK_SIZE (16)
|
|
|
|
#define MF_CLASSIC_TOTAL_BLOCKS_MAX (256)
|
2023-01-27 05:51:47 +00:00
|
|
|
#define MF_MINI_TOTAL_SECTORS_NUM (5)
|
2022-03-23 22:14:34 +00:00
|
|
|
#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)
|
2022-05-24 14:00:15 +00:00
|
|
|
#define MF_CLASSIC_MAX_DATA_SIZE (16)
|
2022-07-26 15:30:49 +00:00
|
|
|
#define MF_CLASSIC_KEY_SIZE (6)
|
|
|
|
#define MF_CLASSIC_ACCESS_BYTES_SIZE (4)
|
2022-03-23 22:14:34 +00:00
|
|
|
|
|
|
|
typedef enum {
|
|
|
|
MfClassicType1k,
|
|
|
|
MfClassicType4k,
|
2023-01-27 05:51:47 +00:00
|
|
|
MfClassicTypeMini,
|
2022-03-23 22:14:34 +00:00
|
|
|
} MfClassicType;
|
|
|
|
|
|
|
|
typedef enum {
|
|
|
|
MfClassicKeyA,
|
|
|
|
MfClassicKeyB,
|
|
|
|
} MfClassicKey;
|
|
|
|
|
2022-10-28 16:10:16 +00:00
|
|
|
typedef enum {
|
|
|
|
MfClassicActionDataRead,
|
|
|
|
MfClassicActionDataWrite,
|
|
|
|
MfClassicActionDataInc,
|
|
|
|
MfClassicActionDataDec,
|
|
|
|
|
|
|
|
MfClassicActionKeyARead,
|
|
|
|
MfClassicActionKeyAWrite,
|
|
|
|
MfClassicActionKeyBRead,
|
|
|
|
MfClassicActionKeyBWrite,
|
|
|
|
MfClassicActionACRead,
|
|
|
|
MfClassicActionACWrite,
|
|
|
|
} MfClassicAction;
|
|
|
|
|
2022-03-23 22:14:34 +00:00
|
|
|
typedef struct {
|
|
|
|
uint8_t value[MF_CLASSIC_BLOCK_SIZE];
|
|
|
|
} MfClassicBlock;
|
|
|
|
|
|
|
|
typedef struct {
|
2022-07-26 15:30:49 +00:00
|
|
|
uint8_t key_a[MF_CLASSIC_KEY_SIZE];
|
|
|
|
uint8_t access_bits[MF_CLASSIC_ACCESS_BYTES_SIZE];
|
|
|
|
uint8_t key_b[MF_CLASSIC_KEY_SIZE];
|
2022-03-23 22:14:34 +00:00
|
|
|
} MfClassicSectorTrailer;
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
uint8_t total_blocks;
|
|
|
|
MfClassicBlock block[MF_CLASSIC_BLOCKS_IN_SECTOR_MAX];
|
|
|
|
} MfClassicSector;
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
MfClassicType type;
|
2022-07-26 15:30:49 +00:00
|
|
|
uint32_t block_read_mask[MF_CLASSIC_TOTAL_BLOCKS_MAX / 32];
|
2022-05-24 14:00:15 +00:00
|
|
|
uint64_t key_a_mask;
|
|
|
|
uint64_t key_b_mask;
|
2022-03-23 22:14:34 +00:00
|
|
|
MfClassicBlock block[MF_CLASSIC_TOTAL_BLOCKS_MAX];
|
|
|
|
} MfClassicData;
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
uint8_t sector;
|
|
|
|
uint64_t key_a;
|
|
|
|
uint64_t key_b;
|
|
|
|
} MfClassicAuthContext;
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
uint8_t sector_num;
|
|
|
|
uint64_t key_a;
|
|
|
|
uint64_t key_b;
|
|
|
|
} MfClassicSectorReader;
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
MfClassicType type;
|
|
|
|
Crypto1 crypto;
|
2022-07-26 15:30:49 +00:00
|
|
|
uint8_t sectors_to_read;
|
2022-03-23 22:14:34 +00:00
|
|
|
MfClassicSectorReader sector_reader[MF_CLASSIC_SECTORS_MAX];
|
|
|
|
} MfClassicReader;
|
|
|
|
|
2022-05-24 14:00:15 +00:00
|
|
|
typedef struct {
|
|
|
|
uint32_t cuid;
|
|
|
|
Crypto1 crypto;
|
|
|
|
MfClassicData data;
|
|
|
|
bool data_changed;
|
|
|
|
} MfClassicEmulator;
|
|
|
|
|
2022-07-26 15:30:49 +00:00
|
|
|
const char* mf_classic_get_type_str(MfClassicType type);
|
|
|
|
|
2022-03-23 22:14:34 +00:00
|
|
|
bool mf_classic_check_card_type(uint8_t ATQA0, uint8_t ATQA1, uint8_t SAK);
|
|
|
|
|
2023-01-27 08:21:52 +00:00
|
|
|
MfClassicType mf_classic_get_classic_type(uint8_t ATQA0, uint8_t ATQA1, uint8_t SAK);
|
2022-07-26 15:30:49 +00:00
|
|
|
|
|
|
|
uint8_t mf_classic_get_total_sectors_num(MfClassicType type);
|
2022-03-23 22:14:34 +00:00
|
|
|
|
2022-11-10 16:20:35 +00:00
|
|
|
uint16_t mf_classic_get_total_block_num(MfClassicType type);
|
|
|
|
|
2022-07-26 15:30:49 +00:00
|
|
|
uint8_t mf_classic_get_sector_trailer_block_num_by_sector(uint8_t sector);
|
|
|
|
|
|
|
|
bool mf_classic_is_sector_trailer(uint8_t block);
|
|
|
|
|
|
|
|
uint8_t mf_classic_get_sector_by_block(uint8_t block);
|
|
|
|
|
2022-10-28 16:10:16 +00:00
|
|
|
bool mf_classic_is_allowed_access_sector_trailer(
|
|
|
|
MfClassicData* data,
|
|
|
|
uint8_t block_num,
|
|
|
|
MfClassicKey key,
|
|
|
|
MfClassicAction action);
|
|
|
|
|
|
|
|
bool mf_classic_is_allowed_access_data_block(
|
|
|
|
MfClassicData* data,
|
|
|
|
uint8_t block_num,
|
|
|
|
MfClassicKey key,
|
|
|
|
MfClassicAction action);
|
|
|
|
|
2023-02-07 03:21:25 +00:00
|
|
|
bool mf_classic_is_value_block(MfClassicData* data, uint8_t block_num);
|
|
|
|
|
|
|
|
bool mf_classic_block_to_value(const uint8_t* block, int32_t* value, uint8_t* addr);
|
|
|
|
|
|
|
|
void mf_classic_value_to_block(int32_t value, uint8_t addr, uint8_t* block);
|
|
|
|
|
2022-07-26 15:30:49 +00:00
|
|
|
bool mf_classic_is_key_found(MfClassicData* data, uint8_t sector_num, MfClassicKey key_type);
|
|
|
|
|
|
|
|
void mf_classic_set_key_found(
|
|
|
|
MfClassicData* data,
|
|
|
|
uint8_t sector_num,
|
|
|
|
MfClassicKey key_type,
|
|
|
|
uint64_t key);
|
|
|
|
|
2022-10-13 16:23:29 +00:00
|
|
|
void mf_classic_set_key_not_found(MfClassicData* data, uint8_t sector_num, MfClassicKey key_type);
|
|
|
|
|
2022-07-26 15:30:49 +00:00
|
|
|
bool mf_classic_is_block_read(MfClassicData* data, uint8_t block_num);
|
|
|
|
|
|
|
|
void mf_classic_set_block_read(MfClassicData* data, uint8_t block_num, MfClassicBlock* block_data);
|
|
|
|
|
2022-10-28 16:10:16 +00:00
|
|
|
bool mf_classic_is_sector_data_read(MfClassicData* data, uint8_t sector_num);
|
|
|
|
|
|
|
|
void mf_classic_set_sector_data_not_read(MfClassicData* data);
|
|
|
|
|
2022-07-26 15:30:49 +00:00
|
|
|
bool mf_classic_is_sector_read(MfClassicData* data, uint8_t sector_num);
|
|
|
|
|
2022-10-13 16:23:29 +00:00
|
|
|
bool mf_classic_is_card_read(MfClassicData* data);
|
|
|
|
|
2022-07-26 15:30:49 +00:00
|
|
|
void mf_classic_get_read_sectors_and_keys(
|
|
|
|
MfClassicData* data,
|
|
|
|
uint8_t* sectors_read,
|
|
|
|
uint8_t* keys_found);
|
|
|
|
|
|
|
|
MfClassicSectorTrailer*
|
|
|
|
mf_classic_get_sector_trailer_by_sector(MfClassicData* data, uint8_t sector);
|
|
|
|
|
|
|
|
void mf_classic_auth_init_context(MfClassicAuthContext* auth_ctx, uint8_t sector);
|
|
|
|
|
|
|
|
bool mf_classic_authenticate(
|
|
|
|
FuriHalNfcTxRxContext* tx_rx,
|
|
|
|
uint8_t block_num,
|
|
|
|
uint64_t key,
|
|
|
|
MfClassicKey key_type);
|
2022-03-23 22:14:34 +00:00
|
|
|
|
2022-12-29 05:20:01 +00:00
|
|
|
bool mf_classic_authenticate_skip_activate(
|
|
|
|
FuriHalNfcTxRxContext* tx_rx,
|
|
|
|
uint8_t block_num,
|
|
|
|
uint64_t key,
|
|
|
|
MfClassicKey key_type,
|
|
|
|
bool skip_activate,
|
|
|
|
uint32_t cuid);
|
|
|
|
|
2022-03-23 22:14:34 +00:00
|
|
|
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);
|
|
|
|
|
2023-02-07 03:21:25 +00:00
|
|
|
bool mf_classic_read_block(
|
|
|
|
FuriHalNfcTxRxContext* tx_rx,
|
|
|
|
Crypto1* crypto,
|
|
|
|
uint8_t block_num,
|
|
|
|
MfClassicBlock* block);
|
|
|
|
|
2022-07-26 15:30:49 +00:00
|
|
|
void mf_classic_read_sector(FuriHalNfcTxRxContext* tx_rx, MfClassicData* data, uint8_t sec_num);
|
2022-03-23 22:14:34 +00:00
|
|
|
|
|
|
|
uint8_t mf_classic_read_card(
|
|
|
|
FuriHalNfcTxRxContext* tx_rx,
|
|
|
|
MfClassicReader* reader,
|
|
|
|
MfClassicData* data);
|
2022-05-24 14:00:15 +00:00
|
|
|
|
2022-07-26 15:30:49 +00:00
|
|
|
uint8_t mf_classic_update_card(FuriHalNfcTxRxContext* tx_rx, MfClassicData* data);
|
|
|
|
|
2022-05-24 14:00:15 +00:00
|
|
|
bool mf_classic_emulator(MfClassicEmulator* emulator, FuriHalNfcTxRxContext* tx_rx);
|
2022-10-28 16:10:16 +00:00
|
|
|
|
2023-02-07 03:21:25 +00:00
|
|
|
void mf_classic_halt(FuriHalNfcTxRxContext* tx_rx, Crypto1* crypto);
|
|
|
|
|
2022-10-28 16:10:16 +00:00
|
|
|
bool mf_classic_write_block(
|
2023-02-07 03:21:25 +00:00
|
|
|
FuriHalNfcTxRxContext* tx_rx,
|
|
|
|
Crypto1* crypto,
|
|
|
|
uint8_t block_num,
|
|
|
|
MfClassicBlock* src_block);
|
|
|
|
|
|
|
|
bool mf_classic_auth_write_block(
|
2022-10-28 16:10:16 +00:00
|
|
|
FuriHalNfcTxRxContext* tx_rx,
|
|
|
|
MfClassicBlock* src_block,
|
|
|
|
uint8_t block_num,
|
|
|
|
MfClassicKey key_type,
|
|
|
|
uint64_t key);
|
|
|
|
|
2023-02-07 03:21:25 +00:00
|
|
|
bool mf_classic_transfer(FuriHalNfcTxRxContext* tx_rx, Crypto1* crypto, uint8_t block_num);
|
|
|
|
|
|
|
|
bool mf_classic_value_cmd(
|
|
|
|
FuriHalNfcTxRxContext* tx_rx,
|
|
|
|
Crypto1* crypto,
|
|
|
|
uint8_t block_num,
|
|
|
|
uint8_t cmd,
|
|
|
|
int32_t d_value);
|
|
|
|
|
|
|
|
bool mf_classic_value_cmd_full(
|
|
|
|
FuriHalNfcTxRxContext* tx_rx,
|
|
|
|
MfClassicBlock* src_block,
|
|
|
|
uint8_t block_num,
|
|
|
|
MfClassicKey key_type,
|
|
|
|
uint64_t key,
|
|
|
|
int32_t d_value);
|
|
|
|
|
2022-10-28 16:10:16 +00:00
|
|
|
bool mf_classic_write_sector(
|
|
|
|
FuriHalNfcTxRxContext* tx_rx,
|
|
|
|
MfClassicData* dest_data,
|
|
|
|
MfClassicData* src_data,
|
|
|
|
uint8_t sec_num);
|