f38a1528 |
1 | #include "mifaredesfire.h" |
0ec548dc |
2 | #include "BigBuf.h" |
f38a1528 |
3 | |
4 | #define MAX_APPLICATION_COUNT 28 |
5 | #define MAX_FILE_COUNT 16 |
a501c82b |
6 | #define MAX_DESFIRE_FRAME_SIZE 60 |
f38a1528 |
7 | #define NOT_YET_AUTHENTICATED 255 |
a501c82b |
8 | #define FRAME_PAYLOAD_SIZE (MAX_DESFIRE_FRAME_SIZE - 5) |
9 | #define RECEIVE_SIZE 64 |
f38a1528 |
10 | |
313ee67e |
11 | // the block number for the ISO14443-4 PCB |
12 | uint8_t pcb_blocknum = 0; |
13 | // Deselect card by sending a s-block. the crc is precalced for speed |
14 | static uint8_t deselect_cmd[] = {0xc2,0xe0,0xb4}; |
15 | |
f38a1528 |
16 | //static uint8_t __msg[MAX_FRAME_SIZE] = { 0x0A, 0x00, 0x00, /* ..., */ 0x00 }; |
17 | /* PCB CID CMD PAYLOAD */ |
18 | //static uint8_t __res[MAX_FRAME_SIZE]; |
19 | |
313ee67e |
20 | bool InitDesfireCard(){ |
e3ab50ca |
21 | |
f8850434 |
22 | iso14a_card_select_t card; |
23 | |
313ee67e |
24 | iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN); |
99cf19d9 |
25 | set_tracing(TRUE); |
26 | |
f8850434 |
27 | if (!iso14443a_select_card(NULL, &card, NULL, true, 0)) { |
28 | if (MF_DBGLEVEL >= MF_DBG_ERROR) DbpString("Can't select card"); |
225ccb91 |
29 | OnError(1); |
313ee67e |
30 | return false; |
31 | } |
32 | return true; |
33 | } |
34 | |
75465377 |
35 | // ARG0 flag enums |
36 | enum { |
37 | NONE = 0x00, |
38 | INIT = 0x01, |
39 | DISCONNECT = 0x02, |
f6c18637 |
40 | CLEARTRACE = 0x04, |
75465377 |
41 | BAR = 0x08, |
42 | } CmdOptions ; |
43 | |
313ee67e |
44 | void MifareSendCommand(uint8_t arg0, uint8_t arg1, uint8_t *datain){ |
45 | |
46 | /* ARG0 contains flags. |
47 | 0x01 = init card. |
f6c18637 |
48 | 0x02 = Disconnect |
313ee67e |
49 | 0x03 |
50 | */ |
51 | uint8_t flags = arg0; |
52 | size_t datalen = arg1; |
a501c82b |
53 | uint8_t resp[RECEIVE_SIZE]; |
313ee67e |
54 | memset(resp,0,sizeof(resp)); |
55 | |
56 | if (MF_DBGLEVEL >= 4) { |
75465377 |
57 | Dbprintf(" flags : %02X", flags); |
58 | Dbprintf(" len : %02X", datalen); |
59 | print_result(" RX : ", datain, datalen); |
313ee67e |
60 | } |
61 | |
810f5379 |
62 | if ( flags & CLEARTRACE ) |
0ec548dc |
63 | clear_trace(); |
f6c18637 |
64 | |
75465377 |
65 | if ( flags & INIT ){ |
313ee67e |
66 | if ( !InitDesfireCard() ) |
67 | return; |
68 | } |
69 | |
70 | int len = DesfireAPDU(datain, datalen, resp); |
810f5379 |
71 | if (MF_DBGLEVEL >= 4) |
f5ed4d12 |
72 | print_result("ERR <--: ", resp, len); |
f6c18637 |
73 | |
74 | if ( !len ) { |
225ccb91 |
75 | OnError(2); |
313ee67e |
76 | return; |
77 | } |
313ee67e |
78 | |
75465377 |
79 | // reset the pcb_blocknum, |
80 | pcb_blocknum = 0; |
81 | |
810f5379 |
82 | if ( flags & DISCONNECT ) |
75465377 |
83 | OnSuccess(); |
84 | |
85 | cmd_send(CMD_ACK,1,len,0,resp,len); |
313ee67e |
86 | } |
87 | |
88 | void MifareDesfireGetInformation(){ |
89 | |
90 | int len = 0; |
f8850434 |
91 | iso14a_card_select_t card; |
e3ab50ca |
92 | uint8_t resp[USB_CMD_DATA_SIZE] = {0x00}; |
93 | uint8_t dataout[USB_CMD_DATA_SIZE] = {0x00}; |
f38a1528 |
94 | |
95 | /* |
96 | 1 = PCB 1 |
97 | 2 = cid 2 |
98 | 3 = desfire command 3 |
99 | 4-5 = crc 4 key |
313ee67e |
100 | 5-6 crc |
f38a1528 |
101 | PCB == 0x0A because sending CID byte. |
313ee67e |
102 | CID == 0x00 first card? |
f38a1528 |
103 | */ |
0ec548dc |
104 | clear_trace(); |
105 | set_tracing(TRUE); |
f38a1528 |
106 | iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN); |
107 | |
108 | // card select - information |
f8850434 |
109 | if ( !iso14443a_select_card(NULL, &card, NULL, true, 0) ) { |
110 | if (MF_DBGLEVEL >= MF_DBG_ERROR) DbpString("Can't select card"); |
225ccb91 |
111 | OnError(1); |
f38a1528 |
112 | return; |
113 | } |
114 | |
f8850434 |
115 | memcpy(dataout, card.uid, 7); |
f38a1528 |
116 | |
117 | LED_A_ON(); |
118 | LED_B_OFF(); |
119 | LED_C_OFF(); |
120 | |
313ee67e |
121 | uint8_t cmd[] = {GET_VERSION}; |
122 | size_t cmd_len = sizeof(cmd); |
123 | |
124 | len = DesfireAPDU(cmd, cmd_len, resp); |
125 | if ( !len ) { |
f38a1528 |
126 | print_result("ERROR <--: ", resp, len); |
225ccb91 |
127 | OnError(2); |
f38a1528 |
128 | return; |
129 | } |
313ee67e |
130 | |
131 | LED_A_OFF(); |
132 | LED_B_ON(); |
f38a1528 |
133 | memcpy(dataout+7,resp+3,7); |
134 | |
135 | // ADDITION_FRAME 1 |
313ee67e |
136 | cmd[0] = ADDITIONAL_FRAME; |
137 | len = DesfireAPDU(cmd, cmd_len, resp); |
138 | if ( !len ) { |
f38a1528 |
139 | print_result("ERROR <--: ", resp, len); |
225ccb91 |
140 | OnError(2); |
f38a1528 |
141 | return; |
142 | } |
313ee67e |
143 | |
144 | LED_B_OFF(); |
145 | LED_C_ON(); |
f38a1528 |
146 | memcpy(dataout+7+7,resp+3,7); |
147 | |
148 | // ADDITION_FRAME 2 |
313ee67e |
149 | len = DesfireAPDU(cmd, cmd_len, resp); |
150 | if ( !len ) { |
f38a1528 |
151 | print_result("ERROR <--: ", resp, len); |
225ccb91 |
152 | OnError(2); |
f38a1528 |
153 | return; |
154 | } |
155 | |
156 | memcpy(dataout+7+7+7,resp+3,14); |
157 | |
f38a1528 |
158 | cmd_send(CMD_ACK,1,0,0,dataout,sizeof(dataout)); |
313ee67e |
159 | |
160 | // reset the pcb_blocknum, |
161 | pcb_blocknum = 0; |
f38a1528 |
162 | OnSuccess(); |
163 | } |
164 | |
165 | void MifareDES_Auth1(uint8_t mode, uint8_t algo, uint8_t keyno, uint8_t *datain){ |
166 | |
f6c18637 |
167 | int len = 0; |
168 | //uint8_t PICC_MASTER_KEY8[8] = { 0x40,0x41,0x42,0x43,0x44,0x45,0x46,0x47}; |
169 | uint8_t PICC_MASTER_KEY16[16] = { 0x40,0x41,0x42,0x43,0x44,0x45,0x46,0x47,0x48,0x49,0x4a,0x4b,0x4c,0x4d,0x4e,0x4f }; |
b6f41bfd |
170 | uint8_t null_key_data8[8] = {0x00}; |
f6c18637 |
171 | //uint8_t null_key_data16[16] = {0x00}; |
172 | //uint8_t new_key_data8[8] = { 0x00,0x11,0x22,0x33,0x44,0x55,0x66,0x77}; |
173 | //uint8_t new_key_data16[16] = { 0x00,0x11,0x22,0x33,0x44,0x55,0x66,0x77,0x88,0x99,0xAA,0xBB,0xCC,0xDD,0xEE,0xFF}; |
f38a1528 |
174 | |
f6c18637 |
175 | uint8_t resp[256] = {0x00}; |
176 | uint8_t IV[16] = {0x00}; |
f38a1528 |
177 | |
f6c18637 |
178 | size_t datalen = datain[0]; |
179 | |
180 | uint8_t cmd[40] = {0x00}; |
181 | uint8_t encRndB[16] = {0x00}; |
182 | uint8_t decRndB[16] = {0x00}; |
183 | uint8_t nonce[16] = {0x00}; |
184 | uint8_t both[32] = {0x00}; |
185 | uint8_t encBoth[32] = {0x00}; |
f38a1528 |
186 | |
f6c18637 |
187 | InitDesfireCard(); |
f38a1528 |
188 | |
857bc2ff |
189 | LED_A_ON(); |
190 | LED_B_OFF(); |
191 | LED_C_OFF(); |
192 | |
f38a1528 |
193 |