|
| 1 | +/** |
| 2 | + * @file mf_classic.h |
| 3 | + * @brief MIFARE Classic auth, read, write sectors. |
| 4 | + * |
| 5 | + * Fixed implementation matching Flipper Zero auth flow. |
| 6 | + */ |
| 7 | +#ifndef MF_CLASSIC_H |
| 8 | +#define MF_CLASSIC_H |
| 9 | + |
| 10 | +#include <stdint.h> |
| 11 | +#include "highboy_nfc_types.h" |
| 12 | +#include "highboy_nfc_error.h" |
| 13 | +#include "crypto1.h" |
| 14 | + |
| 15 | +/** Authenticate a sector with key A or B. */ |
| 16 | +hb_nfc_err_t mf_classic_auth(uint8_t block, mf_key_type_t key_type, |
| 17 | + const mf_classic_key_t* key, |
| 18 | + const uint8_t uid[4]); |
| 19 | + |
| 20 | +/** Read a single block (16 bytes). Must be authenticated first. */ |
| 21 | +hb_nfc_err_t mf_classic_read_block(uint8_t block, uint8_t data[16]); |
| 22 | + |
| 23 | +/** Write a single block (16 bytes). Must be authenticated first. */ |
| 24 | +hb_nfc_err_t mf_classic_write_block(uint8_t block, const uint8_t data[16]); |
| 25 | + |
| 26 | +/** Write phase (for debugging NACKs). */ |
| 27 | +typedef enum { |
| 28 | + MF_WRITE_PHASE_NONE = 0, |
| 29 | + MF_WRITE_PHASE_CMD, |
| 30 | + MF_WRITE_PHASE_DATA, |
| 31 | +} mf_write_phase_t; |
| 32 | + |
| 33 | +/** Get last write phase reached (CMD or DATA). */ |
| 34 | +mf_write_phase_t mf_classic_get_last_write_phase(void); |
| 35 | + |
| 36 | +/** Get card type from SAK. */ |
| 37 | +mf_classic_type_t mf_classic_get_type(uint8_t sak); |
| 38 | + |
| 39 | +/** Get number of sectors for a given type. */ |
| 40 | +int mf_classic_get_sector_count(mf_classic_type_t type); |
| 41 | + |
| 42 | +/** Reset auth state (call before re-select). */ |
| 43 | +void mf_classic_reset_auth(void); |
| 44 | + |
| 45 | +/** Get the last nonce (nt) received from card during auth. |
| 46 | + * Used for PRNG analysis / clone detection. */ |
| 47 | +uint32_t mf_classic_get_last_nt(void); |
| 48 | + |
| 49 | +/** |
| 50 | + * Copy the current Crypto1 cipher state (after a successful mf_classic_auth). |
| 51 | + * Used by the nested attack module to drive its own keystream without |
| 52 | + * touching the static s_crypto that mf_classic_read/write_block use. |
| 53 | + * |
| 54 | + * @param out Destination state struct; must not be NULL. |
| 55 | + */ |
| 56 | +void mf_classic_get_crypto_state(crypto1_state_t* out); |
| 57 | + |
| 58 | +#endif |
0 commit comments