unleashed-firmware/lib/nfc/protocols/mifare_classic.h
gornekich 93a6e17ce5
[FL-2933] Mf Classic initial write, update, detect reader (#1941)
* nfc: introduce nfc write
* nfc: add write logic
* nfc worker: add write state
* nfc: add mfc update logic
* nfc: add update success logic
* nfc: add custom card for detect reader
* nfc: update write logic
* nfc: add halt command, add notifications
* nfc: add write fail scene
* nfc: fixes and clean up
* nfc: fix navigation ad notifications
* nfc: fix detect reader nfc data setter

Co-authored-by: あく <alleteam@gmail.com>
2022-10-29 01:10:16 +09:00

191 lines
4.9 KiB
C

#pragma once
#include <furi_hal_nfc.h>
#include "crypto1.h"
#define MF_CLASSIC_BLOCK_SIZE (16)
#define MF_CLASSIC_TOTAL_BLOCKS_MAX (256)
#define MF_CLASSIC_1K_TOTAL_SECTORS_NUM (16)
#define MF_CLASSIC_4K_TOTAL_SECTORS_NUM (40)
#define MF_CLASSIC_SECTORS_MAX (40)
#define MF_CLASSIC_BLOCKS_IN_SECTOR_MAX (16)
#define MF_CLASSIC_NO_KEY (0xFFFFFFFFFFFFFFFF)
#define MF_CLASSIC_MAX_DATA_SIZE (16)
#define MF_CLASSIC_KEY_SIZE (6)
#define MF_CLASSIC_ACCESS_BYTES_SIZE (4)
typedef enum {
MfClassicType1k,
MfClassicType4k,
} MfClassicType;
typedef enum {
MfClassicKeyA,
MfClassicKeyB,
} MfClassicKey;
typedef enum {
MfClassicActionDataRead,
MfClassicActionDataWrite,
MfClassicActionDataInc,
MfClassicActionDataDec,
MfClassicActionKeyARead,
MfClassicActionKeyAWrite,
MfClassicActionKeyBRead,
MfClassicActionKeyBWrite,
MfClassicActionACRead,
MfClassicActionACWrite,
} MfClassicAction;
typedef struct {
uint8_t value[MF_CLASSIC_BLOCK_SIZE];
} MfClassicBlock;
typedef struct {
uint8_t key_a[MF_CLASSIC_KEY_SIZE];
uint8_t access_bits[MF_CLASSIC_ACCESS_BYTES_SIZE];
uint8_t key_b[MF_CLASSIC_KEY_SIZE];
} MfClassicSectorTrailer;
typedef struct {
uint8_t total_blocks;
MfClassicBlock block[MF_CLASSIC_BLOCKS_IN_SECTOR_MAX];
} MfClassicSector;
typedef struct {
MfClassicType type;
uint32_t block_read_mask[MF_CLASSIC_TOTAL_BLOCKS_MAX / 32];
uint64_t key_a_mask;
uint64_t key_b_mask;
MfClassicBlock block[MF_CLASSIC_TOTAL_BLOCKS_MAX];
} MfClassicData;
typedef struct {
uint8_t sector;
uint64_t key_a;
uint64_t key_b;
} MfClassicAuthContext;
typedef struct {
uint8_t sector_num;
uint64_t key_a;
uint64_t key_b;
} MfClassicSectorReader;
typedef struct {
MfClassicType type;
Crypto1 crypto;
uint8_t sectors_to_read;
MfClassicSectorReader sector_reader[MF_CLASSIC_SECTORS_MAX];
} MfClassicReader;
typedef struct {
uint32_t cuid;
Crypto1 crypto;
MfClassicData data;
bool data_changed;
} MfClassicEmulator;
const char* mf_classic_get_type_str(MfClassicType type);
bool mf_classic_check_card_type(uint8_t ATQA0, uint8_t ATQA1, uint8_t SAK);
MfClassicType mf_classic_get_classic_type(int8_t ATQA0, uint8_t ATQA1, uint8_t SAK);
uint8_t mf_classic_get_total_sectors_num(MfClassicType type);
uint8_t mf_classic_get_sector_trailer_block_num_by_sector(uint8_t sector);
bool mf_classic_is_sector_trailer(uint8_t block);
uint8_t mf_classic_get_sector_by_block(uint8_t block);
bool mf_classic_is_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);
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);
void mf_classic_set_key_not_found(MfClassicData* data, uint8_t sector_num, MfClassicKey key_type);
bool mf_classic_is_block_read(MfClassicData* data, uint8_t block_num);
void mf_classic_set_block_read(MfClassicData* data, uint8_t block_num, MfClassicBlock* block_data);
bool mf_classic_is_sector_data_read(MfClassicData* data, uint8_t sector_num);
void mf_classic_set_sector_data_not_read(MfClassicData* data);
bool mf_classic_is_sector_read(MfClassicData* data, uint8_t sector_num);
bool mf_classic_is_card_read(MfClassicData* data);
void mf_classic_get_read_sectors_and_keys(
MfClassicData* data,
uint8_t* sectors_read,
uint8_t* keys_found);
MfClassicSectorTrailer*
mf_classic_get_sector_trailer_by_sector(MfClassicData* data, uint8_t sector);
void mf_classic_auth_init_context(MfClassicAuthContext* auth_ctx, uint8_t sector);
bool mf_classic_authenticate(
FuriHalNfcTxRxContext* tx_rx,
uint8_t block_num,
uint64_t key,
MfClassicKey key_type);
bool mf_classic_auth_attempt(
FuriHalNfcTxRxContext* tx_rx,
MfClassicAuthContext* auth_ctx,
uint64_t key);
void mf_classic_reader_add_sector(
MfClassicReader* reader,
uint8_t sector,
uint64_t key_a,
uint64_t key_b);
void mf_classic_read_sector(FuriHalNfcTxRxContext* tx_rx, MfClassicData* data, uint8_t sec_num);
uint8_t mf_classic_read_card(
FuriHalNfcTxRxContext* tx_rx,
MfClassicReader* reader,
MfClassicData* data);
uint8_t mf_classic_update_card(FuriHalNfcTxRxContext* tx_rx, MfClassicData* data);
bool mf_classic_emulator(MfClassicEmulator* emulator, FuriHalNfcTxRxContext* tx_rx);
bool mf_classic_write_block(
FuriHalNfcTxRxContext* tx_rx,
MfClassicBlock* src_block,
uint8_t block_num,
MfClassicKey key_type,
uint64_t key);
bool mf_classic_write_sector(
FuriHalNfcTxRxContext* tx_rx,
MfClassicData* dest_data,
MfClassicData* src_data,
uint8_t sec_num);