1 //-----------------------------------------------------------------------------
3 // Many authors, that makes it possible
5 // This code is licensed to you under the terms of the GNU GPL, version 2 or,
6 // at your option, any later version. See the LICENSE.txt file for the text of
8 //-----------------------------------------------------------------------------
9 // code for work with mifare cards.
10 //-----------------------------------------------------------------------------
12 #ifndef __MIFAREUTIL_H
13 #define __MIFAREUTIL_H
16 #include "crapto1/crapto1.h"
18 // mifare authentication
21 #define CRYPT_REQUEST 2
25 #define AUTHENTICATION_TIMEOUT 848 // card times out 1ms after wrong authentication (according to NXP documentation)
26 #define PRE_AUTHENTICATION_LEADTIME 400 // some (non standard) cards need a pause after select before they are ready for first authentication
28 // reader voltage field detector
29 #define MF_MINFIELDV 4000
31 // Mifare 4k/2k/1k/mini Max Block / Max Sector
32 #define MIFARE_4K_MAXBLOCK 256
33 #define MIFARE_2K_MAXBLOCK 128
34 #define MIFARE_1K_MAXBLOCK 64
35 #define MIFARE_MINI_MAXBLOCK 20
37 #define MIFARE_MINI_MAXSECTOR 5
38 #define MIFARE_1K_MAXSECTOR 16
39 #define MIFARE_2K_MAXSECTOR 32
40 #define MIFARE_4K_MAXSECTOR 40
42 //mifare emulator states
43 #define MFEMUL_NOFIELD 0
45 #define MFEMUL_SELECT 2
46 #define MFEMUL_AUTH1 3
48 #define MFEMUL_WRITEBL2 5
49 #define MFEMUL_INTREG_INC 6
50 #define MFEMUL_INTREG_DEC 7
51 #define MFEMUL_INTREG_REST 8
52 #define MFEMUL_HALTED 9
54 #define cardSTATE_TO_IDLE() cardSTATE = MFEMUL_IDLE; LED_B_OFF(); LED_C_OFF();
56 #ifndef MifareBlockToSector
57 #define MifareBlockToSector(block) (block < 128 ? block / 4 : (block - 128) / 16 + 32)
61 int mifare_sendcmd(uint8_t cmd
, uint8_t *data
, uint8_t data_size
, uint8_t *answer
, uint8_t *answer_parity
, uint32_t *timing
);
62 int mifare_sendcmd_short(struct Crypto1State
*pcs
, uint8_t crypted
, uint8_t cmd
, uint8_t data
, uint8_t *answer
, uint8_t *answer_parity
, uint32_t *timing
);
65 int mifare_classic_auth(struct Crypto1State
*pcs
, uint32_t uid
, uint8_t blockNo
, uint8_t keyType
, uint64_t ui64Key
, uint8_t isNested
);
66 int mifare_classic_authex(struct Crypto1State
*pcs
, uint32_t uid
, uint8_t blockNo
, uint8_t keyType
, uint64_t ui64Key
, uint8_t isNested
, uint32_t *ntptr
, uint32_t *timing
);
67 int mifare_classic_readblock(struct Crypto1State
*pcs
, uint32_t uid
, uint8_t blockNo
, uint8_t *blockData
);
68 int mifare_classic_halt(struct Crypto1State
*pcs
, uint32_t uid
);
69 int mifare_classic_halt_ex(struct Crypto1State
*pcs
);
70 int mifare_classic_writeblock(struct Crypto1State
*pcs
, uint32_t uid
, uint8_t blockNo
, uint8_t *blockData
);
73 int mifare_ul_ev1_auth(uint8_t *keybytes
, uint8_t *pack
);
74 int mifare_ultra_auth(uint8_t *keybytes
);
75 int mifare_ultra_readblock(uint8_t blockNo
, uint8_t *blockData
);
76 int mifare_ultra_writeblock_compat(uint8_t blockNo
, uint8_t *blockData
);
77 int mifare_ultra_writeblock(uint8_t blockNo
, uint8_t *blockData
);
78 int mifare_ultra_halt(void);
81 int mifare_sendcmd_special(struct Crypto1State
*pcs
, uint8_t crypted
, uint8_t cmd
, uint8_t *data
, uint8_t *answer
, uint8_t *answer_parity
, uint32_t *timing
);
82 int mifare_sendcmd_special2(struct Crypto1State
*pcs
, uint8_t crypted
, uint8_t cmd
, uint8_t *data
, uint8_t *answer
, uint8_t *answer_parity
, uint32_t *timing
);
83 int mifare_desfire_des_auth1(uint32_t uid
, uint8_t *blockData
);
84 int mifare_desfire_des_auth2(uint32_t uid
, uint8_t *key
, uint8_t *blockData
);
87 void mf_crypto1_decrypt(struct Crypto1State
*pcs
, uint8_t *data
, int len
);
88 void mf_crypto1_decryptEx(struct Crypto1State
*pcs
, uint8_t *data_in
, int len
, uint8_t *data_out
);
89 void mf_crypto1_encrypt(struct Crypto1State
*pcs
, uint8_t *data
, uint16_t len
, uint8_t *par
);
90 void mf_crypto1_encryptEx(struct Crypto1State
*pcs
, uint8_t *data_in
, uint8_t *keystream
, uint8_t *data_out
, uint16_t len
, uint8_t *par
);
91 uint8_t mf_crypto1_encrypt4bit(struct Crypto1State
*pcs
, uint8_t data
);
93 // Mifare memory structure
94 uint8_t NumBlocksPerSector(uint8_t sectorNo
);
95 uint8_t FirstBlockOfSector(uint8_t sectorNo
);
97 bool IsSectorTrailer(uint8_t blockNo
);
98 uint8_t SectorTrailer(uint8_t blockNo
);
100 // emulator functions
101 void emlClearMem(void);
102 void emlSetMem(uint8_t *data
, int blockNum
, int blocksCount
);
103 void emlSetMem_xt(uint8_t *data
, int blockNum
, int blocksCount
, int blockBtWidth
);
104 void emlGetMem(uint8_t *data
, int blockNum
, int blocksCount
);
105 void emlGetMemBt(uint8_t *data
, int offset
, int byteCount
);
106 uint64_t emlGetKey(int sectorNum
, int keyType
);
107 int emlGetValBl(uint32_t *blReg
, uint8_t *blBlock
, int blockNum
);
108 int emlSetValBl(uint32_t blReg
, uint8_t blBlock
, int blockNum
);
109 int emlCheckValBl(int blockNum
);