]>
Commit | Line | Data |
---|---|---|
20f9a2a1 | 1 | //-----------------------------------------------------------------------------\r |
b62a5a84 | 2 | // Merlok, May 2011, 2012\r |
8f51ddb0 | 3 | // Many authors, whom made it possible\r |
20f9a2a1 M |
4 | //\r |
5 | // This code is licensed to you under the terms of the GNU GPL, version 2 or,\r | |
6 | // at your option, any later version. See the LICENSE.txt file for the text of\r | |
7 | // the license.\r | |
8 | //-----------------------------------------------------------------------------\r | |
8f51ddb0 | 9 | // Work with mifare cards.\r |
20f9a2a1 M |
10 | //-----------------------------------------------------------------------------\r |
11 | \r | |
12 | #include "proxmark3.h"\r | |
13 | #include "apps.h"\r | |
14 | #include "util.h"\r | |
15 | #include "string.h"\r | |
16 | \r | |
17 | #include "iso14443crc.h"\r | |
18 | #include "iso14443a.h"\r | |
19 | #include "crapto1.h"\r | |
20 | #include "mifareutil.h"\r | |
21 | \r | |
f397b5cc M |
22 | int MF_DBGLEVEL = MF_DBG_ALL;\r |
23 | \r | |
8f51ddb0 | 24 | // memory management\r |
6a1f2d82 | 25 | uint8_t* get_bigbufptr_recvrespbuf(void) {\r |
26 | return (((uint8_t *)BigBuf) + RECV_RESP_OFFSET); \r | |
8f51ddb0 | 27 | }\r |
6a1f2d82 | 28 | uint8_t* get_bigbufptr_recvcmdbuf(void) {\r |
8f51ddb0 M |
29 | return (((uint8_t *)BigBuf) + RECV_CMD_OFFSET); \r |
30 | }\r | |
6a1f2d82 | 31 | uint8_t* get_bigbufptr_emlcardmem(void) {\r |
32 | return (((uint8_t *)BigBuf) + CARD_MEMORY_OFFSET);\r | |
8f51ddb0 M |
33 | }\r |
34 | \r | |
35 | // crypto1 helpers\r | |
36 | void mf_crypto1_decrypt(struct Crypto1State *pcs, uint8_t *data, int len){\r | |
37 | uint8_t bt = 0;\r | |
38 | int i;\r | |
39 | \r | |
40 | if (len != 1) {\r | |
41 | for (i = 0; i < len; i++)\r | |
42 | data[i] = crypto1_byte(pcs, 0x00, 0) ^ data[i];\r | |
43 | } else {\r | |
44 | bt = 0;\r | |
45 | for (i = 0; i < 4; i++)\r | |
46 | bt |= (crypto1_bit(pcs, 0, 0) ^ BIT(data[0], i)) << i;\r | |
47 | \r | |
48 | data[0] = bt;\r | |
49 | }\r | |
50 | return;\r | |
51 | }\r | |
52 | \r | |
6a1f2d82 | 53 | void mf_crypto1_encrypt(struct Crypto1State *pcs, uint8_t *data, uint16_t len, uint8_t *par) {\r |
8f51ddb0 M |
54 | uint8_t bt = 0;\r |
55 | int i;\r | |
6a1f2d82 | 56 | par[0] = 0;\r |
8f51ddb0 M |
57 | for (i = 0; i < len; i++) {\r |
58 | bt = data[i];\r | |
59 | data[i] = crypto1_byte(pcs, 0x00, 0) ^ data[i];\r | |
6a1f2d82 | 60 | if((i&0x0007) == 0) par[i>>3] = 0;\r |
61 | par[i>>3] |= (((filter(pcs->odd) ^ oddparity(bt)) & 0x01)<<(7-(i&0x0007)));\r | |
8f51ddb0 M |
62 | } \r |
63 | return;\r | |
64 | }\r | |
65 | \r | |
66 | uint8_t mf_crypto1_encrypt4bit(struct Crypto1State *pcs, uint8_t data) {\r | |
67 | uint8_t bt = 0;\r | |
68 | int i;\r | |
69 | \r | |
70 | for (i = 0; i < 4; i++)\r | |
71 | bt |= (crypto1_bit(pcs, 0, 0) ^ BIT(data, i)) << i;\r | |
72 | \r | |
73 | return bt;\r | |
20f9a2a1 M |
74 | }\r |
75 | \r | |
8f51ddb0 | 76 | // send commands\r |
6a1f2d82 | 77 | 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)\r |
f89c7050 | 78 | {\r |
6a1f2d82 | 79 | return mifare_sendcmd_shortex(pcs, crypted, cmd, data, answer, answer_parity, timing); |
981bd429 | 80 | } |
81 | ||
6a1f2d82 | 82 | int mifare_sendcmd_short_special(struct Crypto1State *pcs, uint8_t crypted, uint8_t cmd, uint8_t* data, uint8_t* answer, uint8_t *answer_parity, uint32_t *timing) |
981bd429 | 83 | { |
6a1f2d82 | 84 | uint8_t dcmd[8];//, ecmd[4]; |
85 | //uint32_t par=0; | |
981bd429 | 86 | |
6a1f2d82 | 87 | dcmd[0] = cmd; |
88 | dcmd[1] = data[0]; | |
981bd429 | 89 | dcmd[2] = data[1]; |
90 | dcmd[3] = data[2]; | |
91 | dcmd[4] = data[3]; | |
92 | dcmd[5] = data[4]; | |
93 | AppendCrc14443a(dcmd, 6); | |
94 | //Dbprintf("Data command: %02x", dcmd[0]); | |
95 | //Dbprintf("Data R: %02x %02x %02x %02x %02x %02x %02x", dcmd[1],dcmd[2],dcmd[3],dcmd[4],dcmd[5],dcmd[6],dcmd[7]); | |
96 | ||
97 | //memcpy(ecmd, dcmd, sizeof(dcmd)); | |
98 | ReaderTransmit(dcmd, sizeof(dcmd), NULL); | |
6a1f2d82 | 99 | int len = ReaderReceive(answer, answer_parity); |
981bd429 | 100 | if(!len) |
101 | { | |
102 | if (MF_DBGLEVEL >= 1) Dbprintf("Authentication failed. Card timeout."); | |
103 | return 2; | |
104 | } | |
105 | return len; | |
106 | } | |
107 | ||
6a1f2d82 | 108 | int mifare_sendcmd_shortex(struct Crypto1State *pcs, uint8_t crypted, uint8_t cmd, uint8_t data, uint8_t *answer, uint8_t *answer_parity, uint32_t *timing) |
981bd429 | 109 | { |
110 | uint8_t dcmd[4], ecmd[4]; | |
6a1f2d82 | 111 | uint16_t pos, res;\r |
112 | uint8_t par[1]; // 1 Byte parity is enough here\r | |
20f9a2a1 M |
113 | dcmd[0] = cmd;\r |
114 | dcmd[1] = data;\r | |
115 | AppendCrc14443a(dcmd, 2);\r | |
116 | \r | |
117 | memcpy(ecmd, dcmd, sizeof(dcmd));\r | |
118 | \r | |
119 | if (crypted) {\r | |
6a1f2d82 | 120 | par[0] = 0;\r |
20f9a2a1 M |
121 | for (pos = 0; pos < 4; pos++)\r |
122 | {\r | |
123 | ecmd[pos] = crypto1_byte(pcs, 0x00, 0) ^ dcmd[pos];\r | |
6a1f2d82 | 124 | par[0] |= (((filter(pcs->odd) ^ oddparity(dcmd[pos])) & 0x01) << (7-pos));\r |
20f9a2a1 M |
125 | } \r |
126 | \r | |
9492e0b0 | 127 | ReaderTransmitPar(ecmd, sizeof(ecmd), par, timing);\r |
20f9a2a1 M |
128 | \r |
129 | } else {\r | |
9492e0b0 | 130 | ReaderTransmit(dcmd, sizeof(dcmd), timing);\r |
20f9a2a1 M |
131 | }\r |
132 | \r | |
6a1f2d82 | 133 | int len = ReaderReceive(answer, par);\r |
134 | \r | |
135 | if (answer_parity) *answer_parity = par[0];\r | |
f89c7050 | 136 | \r |
4abe4f58 | 137 | if (crypted == CRYPT_ALL) {\r |
20f9a2a1 M |
138 | if (len == 1) {\r |
139 | res = 0;\r | |
140 | for (pos = 0; pos < 4; pos++)\r | |
141 | res |= (crypto1_bit(pcs, 0, 0) ^ BIT(answer[0], pos)) << pos;\r | |
142 | \r | |
143 | answer[0] = res;\r | |
144 | \r | |
145 | } else {\r | |
146 | for (pos = 0; pos < len; pos++)\r | |
147 | {\r | |
148 | answer[pos] = crypto1_byte(pcs, 0x00, 0) ^ answer[pos];\r | |
149 | }\r | |
150 | }\r | |
151 | }\r | |
152 | \r | |
153 | return len;\r | |
154 | }\r | |
155 | \r | |
8f51ddb0 | 156 | // mifare commands\r |
6a1f2d82 | 157 | int mifare_classic_auth(struct Crypto1State *pcs, uint32_t uid, uint8_t blockNo, uint8_t keyType, uint64_t ui64Key, uint8_t isNested) \r |
f89c7050 | 158 | {\r |
9492e0b0 | 159 | return mifare_classic_authex(pcs, uid, blockNo, keyType, ui64Key, isNested, NULL, NULL);\r |
f89c7050 M |
160 | }\r |
161 | \r | |
6a1f2d82 | 162 | 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) \r |
20f9a2a1 M |
163 | {\r |
164 | // variables\r | |
4abe4f58 | 165 | int len; \r |
20f9a2a1 M |
166 | uint32_t pos;\r |
167 | uint8_t tmp4[4];\r | |
6a1f2d82 | 168 | uint8_t par[1] = {0};\r |
169 | byte_t nr[4];\r | |
20f9a2a1 M |
170 | uint32_t nt, ntpp; // Supplied tag nonce\r |
171 | \r | |
172 | uint8_t mf_nr_ar[] = { 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 };\r | |
6a1f2d82 | 173 | uint8_t *receivedAnswer = get_bigbufptr_recvrespbuf();\r |
174 | uint8_t *receivedAnswerPar = receivedAnswer + MAX_FRAME_SIZE;\r | |
175 | \r | |
4abe4f58 | 176 | // Transmit MIFARE_CLASSIC_AUTH\r |
6a1f2d82 | 177 | len = mifare_sendcmd_short(pcs, isNested, 0x60 + (keyType & 0x01), blockNo, receivedAnswer, receivedAnswerPar, timing);\r |
178 | if (MF_DBGLEVEL >= 4) Dbprintf("rand tag nonce len: %x", len); \r | |
4abe4f58 | 179 | if (len != 4) return 1;\r |
20f9a2a1 | 180 | \r |
6a1f2d82 | 181 | // "random" reader nonce:\r |
182 | nr[0] = 0x55;\r | |
183 | nr[1] = 0x41;\r | |
184 | nr[2] = 0x49;\r | |
185 | nr[3] = 0x92; \r | |
20f9a2a1 M |
186 | \r |
187 | // Save the tag nonce (nt)\r | |
188 | nt = bytes_to_num(receivedAnswer, 4);\r | |
20f9a2a1 M |
189 | \r |
190 | // ----------------------------- crypto1 create\r | |
4abe4f58 M |
191 | if (isNested)\r |
192 | crypto1_destroy(pcs);\r | |
193 | \r | |
194 | // Init cipher with key\r | |
20f9a2a1 M |
195 | crypto1_create(pcs, ui64Key);\r |
196 | \r | |
4abe4f58 M |
197 | if (isNested == AUTH_NESTED) {\r |
198 | // decrypt nt with help of new key \r | |
199 | nt = crypto1_word(pcs, nt ^ uid, 1) ^ nt;\r | |
200 | } else {\r | |
201 | // Load (plain) uid^nt into the cipher\r | |
202 | crypto1_word(pcs, nt ^ uid, 0);\r | |
203 | }\r | |
204 | \r | |
205 | // some statistic\r | |
f397b5cc | 206 | if (!ntptr && (MF_DBGLEVEL >= 3))\r |
f89c7050 M |
207 | Dbprintf("auth uid: %08x nt: %08x", uid, nt); \r |
208 | \r | |
209 | // save Nt\r | |
210 | if (ntptr)\r | |
211 | *ntptr = nt;\r | |
20f9a2a1 | 212 | \r |
6a1f2d82 | 213 | \r |
4abe4f58 | 214 | // Generate (encrypted) nr+parity by loading it into the cipher (Nr)\r |
6a1f2d82 | 215 | par[0] = 0;\r |
4abe4f58 M |
216 | for (pos = 0; pos < 4; pos++)\r |
217 | {\r | |
6a1f2d82 | 218 | mf_nr_ar[pos] = crypto1_byte(pcs, nr[pos], 0) ^ nr[pos];\r |
219 | par[0] |= (((filter(pcs->odd) ^ oddparity(nr[pos])) & 0x01) << (7-pos));\r | |
4abe4f58 | 220 | } \r |
20f9a2a1 | 221 | \r |
4abe4f58 M |
222 | // Skip 32 bits in pseudo random generator\r |
223 | nt = prng_successor(nt,32);\r | |
20f9a2a1 M |
224 | \r |
225 | // ar+parity\r | |
4abe4f58 M |
226 | for (pos = 4; pos < 8; pos++)\r |
227 | {\r | |
20f9a2a1 | 228 | nt = prng_successor(nt,8);\r |
4abe4f58 | 229 | mf_nr_ar[pos] = crypto1_byte(pcs,0x00,0) ^ (nt & 0xff);\r |
6a1f2d82 | 230 | par[0] |= (((filter(pcs->odd) ^ oddparity(nt & 0xff)) & 0x01) << (7-pos));\r |
4abe4f58 | 231 | } \r |
20f9a2a1 | 232 | \r |
4abe4f58 | 233 | // Transmit reader nonce and reader answer\r |
9492e0b0 | 234 | ReaderTransmitPar(mf_nr_ar, sizeof(mf_nr_ar), par, NULL);\r |
20f9a2a1 | 235 | \r |
6a1f2d82 | 236 | // Receive 4 byte tag answer\r |
237 | len = ReaderReceive(receivedAnswer, receivedAnswerPar);\r | |
4abe4f58 M |
238 | if (!len)\r |
239 | {\r | |
f397b5cc | 240 | if (MF_DBGLEVEL >= 1) Dbprintf("Authentication failed. Card timeout.");\r |
20f9a2a1 | 241 | return 2;\r |
4abe4f58 | 242 | }\r |
20f9a2a1 | 243 | \r |
4abe4f58 | 244 | memcpy(tmp4, receivedAnswer, 4);\r |
20f9a2a1 M |
245 | ntpp = prng_successor(nt, 32) ^ crypto1_word(pcs, 0,0);\r |
246 | \r | |
247 | if (ntpp != bytes_to_num(tmp4, 4)) {\r | |
f397b5cc | 248 | if (MF_DBGLEVEL >= 1) Dbprintf("Authentication failed. Error card response.");\r |
20f9a2a1 M |
249 | return 3;\r |
250 | }\r | |
251 | \r | |
252 | return 0;\r | |
253 | }\r | |
254 | \r | |
255 | int mifare_classic_readblock(struct Crypto1State *pcs, uint32_t uid, uint8_t blockNo, uint8_t *blockData) \r | |
256 | {\r | |
257 | // variables\r | |
4abe4f58 | 258 | int len; \r |
20f9a2a1 M |
259 | uint8_t bt[2];\r |
260 | \r | |
6a1f2d82 | 261 | uint8_t* receivedAnswer = get_bigbufptr_recvrespbuf();\r |
262 | uint8_t* receivedAnswerPar = receivedAnswer + MAX_FRAME_SIZE;\r | |
20f9a2a1 | 263 | \r |
4abe4f58 | 264 | // command MIFARE_CLASSIC_READBLOCK\r |
6a1f2d82 | 265 | len = mifare_sendcmd_short(pcs, 1, 0x30, blockNo, receivedAnswer, receivedAnswerPar, NULL);\r |
20f9a2a1 | 266 | if (len == 1) {\r |
f397b5cc | 267 | if (MF_DBGLEVEL >= 1) Dbprintf("Cmd Error: %02x", receivedAnswer[0]); \r |
20f9a2a1 M |
268 | return 1;\r |
269 | }\r | |
270 | if (len != 18) {\r | |
f397b5cc | 271 | if (MF_DBGLEVEL >= 1) Dbprintf("Cmd Error: card timeout. len: %x", len); \r |
20f9a2a1 M |
272 | return 2;\r |
273 | }\r | |
274 | \r | |
275 | memcpy(bt, receivedAnswer + 16, 2);\r | |
4abe4f58 | 276 | AppendCrc14443a(receivedAnswer, 16);\r |
20f9a2a1 | 277 | if (bt[0] != receivedAnswer[16] || bt[1] != receivedAnswer[17]) {\r |
f397b5cc | 278 | if (MF_DBGLEVEL >= 1) Dbprintf("Cmd CRC response error."); \r |
20f9a2a1 M |
279 | return 3;\r |
280 | }\r | |
281 | \r | |
282 | memcpy(blockData, receivedAnswer, 16);\r | |
981bd429 | 283 | return 0; |
284 | } | |
285 | ||
286 | int mifare_ultra_readblock(uint32_t uid, uint8_t blockNo, uint8_t *blockData) | |
287 | { | |
288 | // variables | |
6a1f2d82 | 289 | uint16_t len; |
981bd429 | 290 | uint8_t bt[2]; |
291 | ||
6a1f2d82 | 292 | uint8_t* receivedAnswer = get_bigbufptr_recvrespbuf();\r |
293 | uint8_t* receivedAnswerPar = receivedAnswer + MAX_FRAME_SIZE; | |
981bd429 | 294 | |
295 | // command MIFARE_CLASSIC_READBLOCK | |
6a1f2d82 | 296 | len = mifare_sendcmd_short(NULL, 1, 0x30, blockNo, receivedAnswer, receivedAnswerPar, NULL); |
981bd429 | 297 | if (len == 1) { |
298 | if (MF_DBGLEVEL >= 1) Dbprintf("Cmd Error: %02x", receivedAnswer[0]); | |
299 | return 1; | |
300 | } | |
301 | if (len != 18) { | |
302 | if (MF_DBGLEVEL >= 1) Dbprintf("Cmd Error: card timeout. len: %x", len); | |
303 | return 2; | |
304 | } | |
305 | ||
306 | memcpy(bt, receivedAnswer + 16, 2); | |
307 | AppendCrc14443a(receivedAnswer, 16); | |
308 | if (bt[0] != receivedAnswer[16] || bt[1] != receivedAnswer[17]) { | |
309 | if (MF_DBGLEVEL >= 1) Dbprintf("Cmd CRC response error."); | |
310 | return 3; | |
311 | } | |
312 | ||
313 | memcpy(blockData, receivedAnswer, 14); | |
314 | return 0; | |
315 | } | |
316 | ||
317 | ||
318 | int mifare_classic_writeblock(struct Crypto1State *pcs, uint32_t uid, uint8_t blockNo, uint8_t *blockData) | |
319 | { | |
320 | // variables | |
4abe4f58 | 321 | int len, i; \r |
20f9a2a1 | 322 | uint32_t pos;\r |
6a1f2d82 | 323 | uint8_t par[3] = {0}; // enough for 18 Bytes to send\r |
4abe4f58 | 324 | byte_t res;\r |
20f9a2a1 M |
325 | \r |
326 | uint8_t d_block[18], d_block_enc[18];\r | |
6a1f2d82 | 327 | uint8_t* receivedAnswer = get_bigbufptr_recvrespbuf();\r |
328 | uint8_t* receivedAnswerPar = receivedAnswer + MAX_FRAME_SIZE;\r | |
20f9a2a1 | 329 | \r |
4abe4f58 | 330 | // command MIFARE_CLASSIC_WRITEBLOCK\r |
6a1f2d82 | 331 | len = mifare_sendcmd_short(pcs, 1, 0xA0, blockNo, receivedAnswer, receivedAnswerPar, NULL);\r |
20f9a2a1 M |
332 | \r |
333 | if ((len != 1) || (receivedAnswer[0] != 0x0A)) { // 0x0a - ACK\r | |
f397b5cc | 334 | if (MF_DBGLEVEL >= 1) Dbprintf("Cmd Error: %02x", receivedAnswer[0]); \r |
20f9a2a1 M |
335 | return 1;\r |
336 | }\r | |
337 | \r | |
338 | memcpy(d_block, blockData, 16);\r | |
339 | AppendCrc14443a(d_block, 16);\r | |
340 | \r | |
341 | // crypto\r | |
4abe4f58 M |
342 | for (pos = 0; pos < 18; pos++)\r |
343 | {\r | |
344 | d_block_enc[pos] = crypto1_byte(pcs, 0x00, 0) ^ d_block[pos];\r | |
6a1f2d82 | 345 | par[pos>>3] |= (((filter(pcs->odd) ^ oddparity(d_block[pos])) & 0x01) << (7 - (pos&0x0007)));\r |
4abe4f58 | 346 | } \r |
20f9a2a1 | 347 | \r |
9492e0b0 | 348 | ReaderTransmitPar(d_block_enc, sizeof(d_block_enc), par, NULL);\r |
20f9a2a1 | 349 | \r |
4abe4f58 | 350 | // Receive the response\r |
6a1f2d82 | 351 | len = ReaderReceive(receivedAnswer, receivedAnswerPar); \r |
20f9a2a1 M |
352 | \r |
353 | res = 0;\r | |
354 | for (i = 0; i < 4; i++)\r | |
355 | res |= (crypto1_bit(pcs, 0, 0) ^ BIT(receivedAnswer[0], i)) << i;\r | |
356 | \r | |
357 | if ((len != 1) || (res != 0x0A)) {\r | |
f397b5cc | 358 | if (MF_DBGLEVEL >= 1) Dbprintf("Cmd send data2 Error: %02x", res); \r |
20f9a2a1 M |
359 | return 2;\r |
360 | }\r | |
361 | \r | |
981bd429 | 362 | return 0; |
363 | } | |
364 | ||
365 | int mifare_ultra_writeblock(uint32_t uid, uint8_t blockNo, uint8_t *blockData) | |
366 | { | |
6a1f2d82 | 367 | // variables |
368 | uint16_t len; | |
369 | uint8_t par[3] = {0}; // enough for 18 parity bits | |
981bd429 | 370 | |
6a1f2d82 | 371 | uint8_t d_block[18]; |
372 | uint8_t* receivedAnswer = get_bigbufptr_recvrespbuf();\r | |
373 | uint8_t* receivedAnswerPar = receivedAnswer + MAX_FRAME_SIZE; | |
981bd429 | 374 | |
6a1f2d82 | 375 | // command MIFARE_CLASSIC_WRITEBLOCK |
376 | len = mifare_sendcmd_short(NULL, true, 0xA0, blockNo, receivedAnswer, receivedAnswerPar, NULL); | |
981bd429 | 377 | |
6a1f2d82 | 378 | if ((len != 1) || (receivedAnswer[0] != 0x0A)) { // 0x0a - ACK |
379 | if (MF_DBGLEVEL >= 1) Dbprintf("Cmd Addr Error: %02x", receivedAnswer[0]); | |
380 | return 1; | |
381 | } | |
981bd429 | 382 | |
383 | memset(d_block,'\0',18); | |
384 | memcpy(d_block, blockData, 16); | |
6a1f2d82 | 385 | AppendCrc14443a(d_block, 16); |
981bd429 | 386 | |
387 | ReaderTransmitPar(d_block, sizeof(d_block), par, NULL); | |
388 | ||
6a1f2d82 | 389 | // Receive the response |
390 | len = ReaderReceive(receivedAnswer, receivedAnswerPar); | |
981bd429 | 391 | |
392 | if ((len != 1) || (receivedAnswer[0] != 0x0A)) { // 0x0a - ACK | |
6a1f2d82 | 393 | if (MF_DBGLEVEL >= 1) Dbprintf("Cmd Data Error: %02x %d", receivedAnswer[0],len); |
394 | return 2; | |
395 | } | |
981bd429 | 396 | |
6a1f2d82 | 397 | return 0; |
981bd429 | 398 | } |
399 | ||
400 | int mifare_ultra_special_writeblock(uint32_t uid, uint8_t blockNo, uint8_t *blockData) | |
401 | { | |
6a1f2d82 | 402 | uint16_t len; |
981bd429 | 403 | |
6a1f2d82 | 404 | uint8_t d_block[8]; |
405 | uint8_t *receivedAnswer = get_bigbufptr_recvrespbuf();\r | |
406 | uint8_t *receivedAnswerPar = receivedAnswer + MAX_FRAME_SIZE; | |
981bd429 | 407 | |
6a1f2d82 | 408 | // command MIFARE_CLASSIC_WRITEBLOCK |
981bd429 | 409 | memset(d_block,'\0',8); |
410 | d_block[0]= blockNo; | |
411 | memcpy(d_block+1,blockData,4); | |
412 | AppendCrc14443a(d_block, 6); | |
413 | ||
414 | //i know the data send here is correct | |
6a1f2d82 | 415 | len = mifare_sendcmd_short_special(NULL, 1, 0xA2, d_block, receivedAnswer, receivedAnswerPar, NULL); |
981bd429 | 416 | |
6a1f2d82 | 417 | if (receivedAnswer[0] != 0x0A) { // 0x0a - ACK |
418 | if (MF_DBGLEVEL >= 1) Dbprintf("Cmd Send Error: %02x %d", receivedAnswer[0],len); | |
419 | return 1; | |
420 | } | |
421 | \r | |
422 | return 0; | |
981bd429 | 423 | } |
424 | ||
425 | int mifare_classic_halt(struct Crypto1State *pcs, uint32_t uid) | |
426 | { | |
427 | // variables | |
6a1f2d82 | 428 | uint16_t len; \r |
20f9a2a1 M |
429 | \r |
430 | // Mifare HALT\r | |
6a1f2d82 | 431 | uint8_t *receivedAnswer = get_bigbufptr_recvrespbuf();\r |
432 | uint8_t *receivedAnswerPar = receivedAnswer + MAX_FRAME_SIZE;\r | |
20f9a2a1 | 433 | \r |
6a1f2d82 | 434 | len = mifare_sendcmd_short(pcs, pcs == NULL ? false:true, 0x50, 0x00, receivedAnswer, receivedAnswerPar, NULL);\r |
4abe4f58 | 435 | if (len != 0) {\r |
f397b5cc | 436 | if (MF_DBGLEVEL >= 1) Dbprintf("halt error. response len: %x", len); \r |
20f9a2a1 M |
437 | return 1;\r |
438 | }\r | |
439 | \r | |
981bd429 | 440 | return 0; |
441 | } | |
442 | ||
443 | int mifare_ultra_halt(uint32_t uid) | |
444 | { | |
6a1f2d82 | 445 | uint16_t len; |
981bd429 | 446 | |
447 | // Mifare HALT | |
6a1f2d82 | 448 | uint8_t *receivedAnswer = get_bigbufptr_recvrespbuf();\r |
449 | uint8_t *receivedAnswerPar = receivedAnswer + MAX_FRAME_SIZE; | |
981bd429 | 450 | |
6a1f2d82 | 451 | len = mifare_sendcmd_short(NULL, true, 0x50, 0x00, receivedAnswer, receivedAnswerPar, NULL); |
981bd429 | 452 | if (len != 0) { |
453 | if (MF_DBGLEVEL >= 1) Dbprintf("halt error. response len: %x", len); | |
454 | return 1; | |
455 | } | |
456 | ||
457 | return 0; | |
458 | } | |
459 | ||
baeaf579 | 460 | \r |
461 | // Mifare Memory Structure: up to 32 Sectors with 4 blocks each (1k and 2k cards),\r | |
462 | // plus evtl. 8 sectors with 16 blocks each (4k cards)\r | |
463 | uint8_t NumBlocksPerSector(uint8_t sectorNo) \r | |
464 | {\r | |
465 | if (sectorNo < 32) \r | |
466 | return 4;\r | |
467 | else\r | |
468 | return 16;\r | |
469 | }\r | |
470 | \r | |
471 | uint8_t FirstBlockOfSector(uint8_t sectorNo) \r | |
472 | {\r | |
473 | if (sectorNo < 32)\r | |
474 | return sectorNo * 4;\r | |
475 | else\r | |
476 | return 32*4 + (sectorNo - 32) * 16;\r | |
477 | \r | |
478 | }\r | |
479 | \r | |
480 | \r | |
981bd429 | 481 | // work with emulator memory |
482 | void emlSetMem(uint8_t *data, int blockNum, int blocksCount) { | |
6a1f2d82 | 483 | uint8_t* emCARD = get_bigbufptr_emlcardmem(); |
8f51ddb0 M |
484 | \r |
485 | memcpy(emCARD + blockNum * 16, data, blocksCount * 16);\r | |
486 | }\r | |
487 | \r | |
488 | void emlGetMem(uint8_t *data, int blockNum, int blocksCount) {\r | |
6a1f2d82 | 489 | uint8_t* emCARD = get_bigbufptr_emlcardmem();\r |
8f51ddb0 M |
490 | \r |
491 | memcpy(data, emCARD + blockNum * 16, blocksCount * 16);\r | |
492 | }\r | |
493 | \r | |
494 | void emlGetMemBt(uint8_t *data, int bytePtr, int byteCount) {\r | |
6a1f2d82 | 495 | uint8_t* emCARD = get_bigbufptr_emlcardmem();\r |
8f51ddb0 M |
496 | \r |
497 | memcpy(data, emCARD + bytePtr, byteCount);\r | |
498 | }\r | |
499 | \r | |
0014cb46 | 500 | int emlCheckValBl(int blockNum) {\r |
6a1f2d82 | 501 | uint8_t* emCARD = get_bigbufptr_emlcardmem();\r |
0014cb46 M |
502 | uint8_t* data = emCARD + blockNum * 16;\r |
503 | \r | |
504 | if ((data[0] != (data[4] ^ 0xff)) || (data[0] != data[8]) ||\r | |
505 | (data[1] != (data[5] ^ 0xff)) || (data[1] != data[9]) ||\r | |
506 | (data[2] != (data[6] ^ 0xff)) || (data[2] != data[10]) ||\r | |
507 | (data[3] != (data[7] ^ 0xff)) || (data[3] != data[11]) ||\r | |
508 | (data[12] != (data[13] ^ 0xff)) || (data[12] != data[14]) ||\r | |
509 | (data[12] != (data[15] ^ 0xff))\r | |
510 | ) \r | |
511 | return 1;\r | |
512 | return 0;\r | |
513 | }\r | |
514 | \r | |
515 | int emlGetValBl(uint32_t *blReg, uint8_t *blBlock, int blockNum) {\r | |
6a1f2d82 | 516 | uint8_t* emCARD = get_bigbufptr_emlcardmem();\r |
0014cb46 M |
517 | uint8_t* data = emCARD + blockNum * 16;\r |
518 | \r | |
519 | if (emlCheckValBl(blockNum)) {\r | |
520 | return 1;\r | |
521 | }\r | |
522 | \r | |
523 | memcpy(blReg, data, 4);\r | |
524 | *blBlock = data[12];\r | |
525 | \r | |
526 | return 0;\r | |
527 | }\r | |
528 | \r | |
529 | int emlSetValBl(uint32_t blReg, uint8_t blBlock, int blockNum) {\r | |
6a1f2d82 | 530 | uint8_t* emCARD = get_bigbufptr_emlcardmem();\r |
0014cb46 M |
531 | uint8_t* data = emCARD + blockNum * 16;\r |
532 | \r | |
533 | memcpy(data + 0, &blReg, 4);\r | |
534 | memcpy(data + 8, &blReg, 4);\r | |
535 | blReg = blReg ^ 0xffffffff;\r | |
536 | memcpy(data + 4, &blReg, 4);\r | |
537 | \r | |
538 | data[12] = blBlock;\r | |
539 | data[13] = blBlock ^ 0xff;\r | |
540 | data[14] = blBlock;\r | |
541 | data[15] = blBlock ^ 0xff;\r | |
542 | \r | |
543 | return 0;\r | |
544 | }\r | |
545 | \r | |
8556b852 M |
546 | uint64_t emlGetKey(int sectorNum, int keyType) {\r |
547 | uint8_t key[6];\r | |
6a1f2d82 | 548 | uint8_t* emCARD = get_bigbufptr_emlcardmem();\r |
8556b852 | 549 | \r |
baeaf579 | 550 | memcpy(key, emCARD + 16 * (FirstBlockOfSector(sectorNum) + NumBlocksPerSector(sectorNum) - 1) + keyType * 10, 6);\r |
8556b852 M |
551 | return bytes_to_num(key, 6);\r |
552 | }\r | |
553 | \r | |
8f51ddb0 | 554 | void emlClearMem(void) {\r |
aea4d766 | 555 | int b;\r |
8f51ddb0 M |
556 | \r |
557 | const uint8_t trailer[] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x07, 0x80, 0x69, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff};\r | |
8f51ddb0 | 558 | const uint8_t uid[] = {0xe6, 0x84, 0x87, 0xf3, 0x16, 0x88, 0x04, 0x00, 0x46, 0x8e, 0x45, 0x55, 0x4d, 0x70, 0x41, 0x04};\r |
6a1f2d82 | 559 | uint8_t* emCARD = get_bigbufptr_emlcardmem();\r |
aea4d766 | 560 | \r |
6a1f2d82 | 561 | memset(emCARD, 0, CARD_MEMORY_SIZE);\r |
aea4d766 | 562 | \r |
563 | // fill sectors trailer data\r | |
564 | for(b = 3; b < 256; b<127?(b+=4):(b+=16)) {\r | |
565 | emlSetMem((uint8_t *)trailer, b , 1);\r | |
566 | } \r | |
8f51ddb0 M |
567 | \r |
568 | // uid\r | |
569 | emlSetMem((uint8_t *)uid, 0, 1);\r | |
570 | return;\r | |
571 | }\r |