]> git.zerfleddert.de Git - proxmark3-svn/blob - armsrc/mifareutil.c
CHG: 'hf iclass replay' added help text.
[proxmark3-svn] / armsrc / mifareutil.c
1 //-----------------------------------------------------------------------------
2 // Merlok, May 2011, 2012
3 // Many authors, whom made it possible
4 //
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
7 // the license.
8 //-----------------------------------------------------------------------------
9 // Work with mifare cards.
10 //-----------------------------------------------------------------------------
11
12 #include "proxmark3.h"
13 #include "apps.h"
14 #include "util.h"
15 #include "string.h"
16
17 #include "iso14443crc.h"
18 #include "iso14443a.h"
19 #include "crapto1.h"
20 #include "mifareutil.h"
21 #include "parity.h"
22 #include "des.h"
23
24 int MF_DBGLEVEL = MF_DBG_ALL;
25
26 // crypto1 helpers
27 void mf_crypto1_decrypt(struct Crypto1State *pcs, uint8_t *data, int len){
28 uint8_t bt = 0;
29 int i;
30
31 if (len != 1) {
32 for (i = 0; i < len; i++)
33 data[i] = crypto1_byte(pcs, 0x00, 0) ^ data[i];
34 } else {
35 bt |= (crypto1_bit(pcs, 0, 0) ^ BIT(data[0], 0)) << 0;
36 bt |= (crypto1_bit(pcs, 0, 0) ^ BIT(data[0], 1)) << 1;
37 bt |= (crypto1_bit(pcs, 0, 0) ^ BIT(data[0], 2)) << 2;
38 bt |= (crypto1_bit(pcs, 0, 0) ^ BIT(data[0], 3)) << 3;
39 data[0] = bt;
40 }
41 return;
42 }
43
44 void mf_crypto1_encrypt(struct Crypto1State *pcs, uint8_t *data, uint16_t len, uint8_t *par) {
45 uint8_t bt = 0;
46 int i;
47 par[0] = 0;
48
49 for (i = 0; i < len; i++) {
50 bt = data[i];
51 data[i] = crypto1_byte(pcs, 0x00, 0) ^ data[i];
52 if ( ( i & 0x0007 ) == 0)
53 par[ i >> 3 ] = 0;
54 par[ i >> 3 ] |= (((filter(pcs->odd) ^ oddparity8(bt)) & 0x01)<<(7-(i&0x0007)));
55 }
56 }
57
58 uint8_t mf_crypto1_encrypt4bit(struct Crypto1State *pcs, uint8_t data) {
59 uint8_t bt = 0;
60 bt |= (crypto1_bit(pcs, 0, 0) ^ BIT(data, 0)) << 0;
61 bt |= (crypto1_bit(pcs, 0, 0) ^ BIT(data, 1)) << 1;
62 bt |= (crypto1_bit(pcs, 0, 0) ^ BIT(data, 2)) << 2;
63 bt |= (crypto1_bit(pcs, 0, 0) ^ BIT(data, 3)) << 3;
64 return bt;
65 }
66
67 // send X byte basic commands
68 int mifare_sendcmd(uint8_t cmd, uint8_t* data, uint8_t data_size, uint8_t* answer, uint8_t *answer_parity, uint32_t *timing) {
69 uint8_t dcmd[data_size+3];
70 dcmd[0] = cmd;
71 memcpy(dcmd+1, data, data_size);
72 AppendCrc14443a(dcmd, data_size+1);
73 ReaderTransmit(dcmd, sizeof(dcmd), timing);
74 int len = ReaderReceive(answer, answer_parity);
75 if(!len) {
76 if (MF_DBGLEVEL >= MF_DBG_ERROR)
77 Dbprintf("%02X Cmd failed. Card timeout.", cmd);
78 len = ReaderReceive(answer,answer_parity);
79 }
80 return len;
81 }
82
83 // send 2 byte commands
84 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) {
85 uint16_t pos, res;
86 uint8_t dcmd[4] = {cmd, data, 0x00, 0x00};
87 uint8_t ecmd[4] = {0x00, 0x00, 0x00, 0x00};
88 uint8_t par[1] = {0x00}; // 1 Byte parity is enough here
89 AppendCrc14443a(dcmd, 2);
90 memcpy(ecmd, dcmd, sizeof(dcmd));
91
92 if (crypted) {
93 par[0] = 0;
94 for (pos = 0; pos < 4; pos++) {
95 ecmd[pos] = crypto1_byte(pcs, 0x00, 0) ^ dcmd[pos];
96 par[0] |= (((filter(pcs->odd) ^ oddparity8(dcmd[pos])) & 0x01) << (7-pos));
97 }
98 ReaderTransmitPar(ecmd, sizeof(ecmd), par, timing);
99 } else {
100 ReaderTransmit(dcmd, sizeof(dcmd), timing);
101 }
102
103 int len = ReaderReceive(answer, par);
104
105 if (answer_parity) *answer_parity = par[0];
106
107 if (crypted == CRYPT_ALL) {
108 if (len == 1) {
109 res = 0;
110 res |= (crypto1_bit(pcs, 0, 0) ^ BIT(answer[0], 0)) << 0;
111 res |= (crypto1_bit(pcs, 0, 0) ^ BIT(answer[0], 1)) << 1;
112 res |= (crypto1_bit(pcs, 0, 0) ^ BIT(answer[0], 2)) << 2;
113 res |= (crypto1_bit(pcs, 0, 0) ^ BIT(answer[0], 3)) << 3;
114 answer[0] = res;
115 } else {
116 for (pos = 0; pos < len; pos++)
117 answer[pos] = crypto1_byte(pcs, 0x00, 0) ^ answer[pos];
118 }
119 }
120 return len;
121 }
122
123 // mifare classic commands
124 int mifare_classic_auth(struct Crypto1State *pcs, uint32_t uid, uint8_t blockNo, uint8_t keyType, uint64_t ui64Key, uint8_t isNested) {
125 return mifare_classic_authex(pcs, uid, blockNo, keyType, ui64Key, isNested, NULL, NULL);
126 }
127
128 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) {
129 int len;
130 uint32_t pos;
131 uint8_t par[1] = {0x00};
132
133 // "random" reader nonce:
134 //byte_t nr[4] = {0x55, 0x41, 0x49, 0x92};
135 byte_t nr[4] = {0x01, 0x01, 0x01, 0x01};
136
137 uint32_t nt, ntpp; // Supplied tag nonce
138
139 uint8_t mf_nr_ar[] = { 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 };
140 uint8_t receivedAnswer[MAX_MIFARE_FRAME_SIZE] = {0x00};
141 uint8_t receivedAnswerPar[MAX_MIFARE_PARITY_SIZE] = {0x00};
142
143 // Transmit MIFARE_CLASSIC_AUTH
144 len = mifare_sendcmd_short(pcs, isNested, 0x60 + (keyType & 0x01), blockNo, receivedAnswer, receivedAnswerPar, timing);
145 if (MF_DBGLEVEL >= 4) Dbprintf("rand tag nonce len: %x", len);
146 if (len != 4) return 1;
147
148 // Save the tag nonce (nt)
149 nt = bytes_to_num(receivedAnswer, 4);
150
151 // ----------------------------- crypto1 create
152 if (isNested)
153 crypto1_destroy(pcs);
154
155 // Init cipher with key
156 crypto1_create(pcs, ui64Key);
157
158 if (isNested == AUTH_NESTED) {
159 // decrypt nt with help of new key
160 nt = crypto1_word(pcs, nt ^ uid, 1) ^ nt;
161 } else {
162 // Load (plain) uid^nt into the cipher
163 crypto1_word(pcs, nt ^ uid, 0);
164 }
165
166 // some statistic
167 if (!ntptr && (MF_DBGLEVEL >= 3))
168 Dbprintf("auth uid: %08x nt: %08x", uid, nt);
169
170 // save Nt
171 if (ntptr)
172 *ntptr = nt;
173
174 // Generate (encrypted) nr+parity by loading it into the cipher (Nr)
175 par[0] = 0;
176 for (pos = 0; pos < 4; pos++) {
177 mf_nr_ar[pos] = crypto1_byte(pcs, nr[pos], 0) ^ nr[pos];
178 par[0] |= (((filter(pcs->odd) ^ oddparity8(nr[pos])) & 0x01) << (7-pos));
179 }
180
181 // Skip 32 bits in pseudo random generator
182 nt = prng_successor(nt, 32);
183
184 // ar+parity
185 for (pos = 4; pos < 8; pos++) {
186 nt = prng_successor(nt,8);
187 mf_nr_ar[pos] = crypto1_byte(pcs,0x00,0) ^ (nt & 0xff);
188 par[0] |= (((filter(pcs->odd) ^ oddparity8(nt & 0xff)) & 0x01) << (7-pos));
189 }
190
191 // Transmit reader nonce and reader answer
192 ReaderTransmitPar(mf_nr_ar, sizeof(mf_nr_ar), par, NULL);
193
194 // Receive 4 byte tag answer
195 len = ReaderReceive(receivedAnswer, receivedAnswerPar);
196 if (!len) {
197 if (MF_DBGLEVEL >= 1) Dbprintf("Authentication failed. Card timeout.");
198 return 2;
199 }
200
201 ntpp = prng_successor(nt, 32) ^ crypto1_word(pcs, 0,0);
202
203 if (ntpp != bytes_to_num(receivedAnswer, 4)) {
204 if (MF_DBGLEVEL >= 1) Dbprintf("Authentication failed. Error card response.");
205 return 3;
206 }
207 return 0;
208 }
209
210 int mifare_classic_readblock(struct Crypto1State *pcs, uint32_t uid, uint8_t blockNo, uint8_t *blockData) {
211
212 int len;
213 uint8_t bt[2] = {0x00, 0x00};
214 uint8_t receivedAnswer[MAX_MIFARE_FRAME_SIZE] = {0x00};
215 uint8_t receivedAnswerPar[MAX_MIFARE_PARITY_SIZE] = {0x00};
216
217 len = mifare_sendcmd_short(pcs, 1, ISO14443A_CMD_READBLOCK, blockNo, receivedAnswer, receivedAnswerPar, NULL);
218 if (len == 1) {
219 if (MF_DBGLEVEL >= 1) Dbprintf("Cmd Error: %02x", receivedAnswer[0]);
220 return 1;
221 }
222 if (len != 18) {
223 if (MF_DBGLEVEL >= 1) Dbprintf("Cmd Error: card timeout. len: %x", len);
224 return 2;
225 }
226
227 memcpy(bt, receivedAnswer + 16, 2);
228 AppendCrc14443a(receivedAnswer, 16);
229 if (bt[0] != receivedAnswer[16] || bt[1] != receivedAnswer[17]) {
230 if (MF_DBGLEVEL >= 1) Dbprintf("Cmd CRC response error.");
231 return 3;
232 }
233
234 memcpy(blockData, receivedAnswer, 16);
235 return 0;
236 }
237
238 // mifare ultralight commands
239 int mifare_ul_ev1_auth(uint8_t *keybytes, uint8_t *pack){
240
241 uint16_t len = 0;
242 uint8_t resp[4] = {0x00, 0x00, 0x00, 0x00};
243 uint8_t respPar[1] = {0x00};
244 uint8_t key[4] = {0x00, 0x00, 0x00, 0x00};
245 memcpy(key, keybytes, 4);
246
247 if (MF_DBGLEVEL >= MF_DBG_EXTENDED)
248 Dbprintf("EV1 Auth : %02x%02x%02x%02x", key[0], key[1], key[2], key[3]);
249
250 len = mifare_sendcmd(0x1B, key, sizeof(key), resp, respPar, NULL);
251
252 if (len != 4) {
253 if (MF_DBGLEVEL >= MF_DBG_ERROR) Dbprintf("Cmd Error: %02x %u", resp[0], len);
254 return 0;
255 }
256
257 if (MF_DBGLEVEL >= MF_DBG_EXTENDED)
258 Dbprintf("Auth Resp: %02x%02x%02x%02x", resp[0],resp[1],resp[2],resp[3]);
259
260 memcpy(pack, resp, 4);
261 return 1;
262 }
263
264 int mifare_ultra_auth(uint8_t *keybytes){
265
266 /// 3des2k
267 uint8_t random_a[8] = {1,1,1,1,1,1,1,1};
268 uint8_t random_b[8] = {0x00};
269 uint8_t enc_random_b[8] = {0x00};
270 uint8_t rnd_ab[16] = {0x00};
271 uint8_t IV[8] = {0x00};
272 uint8_t key[16] = {0x00};
273 memcpy(key, keybytes, 16);
274
275 uint16_t len = 0;
276 uint8_t resp[19] = {0x00};
277 uint8_t respPar[3] = {0,0,0};
278
279 // REQUEST AUTHENTICATION
280 len = mifare_sendcmd_short(NULL, 1, 0x1A, 0x00, resp, respPar ,NULL);
281 if (len != 11) {
282 if (MF_DBGLEVEL >= MF_DBG_ERROR) Dbprintf("Cmd Error: %02x", resp[0]);
283 return 0;
284 }
285
286 // tag nonce.
287 memcpy(enc_random_b,resp+1,8);
288
289 // decrypt nonce.
290 tdes_2key_dec(random_b, enc_random_b, sizeof(random_b), key, IV );
291 rol(random_b,8);
292 memcpy(rnd_ab ,random_a,8);
293 memcpy(rnd_ab+8,random_b,8);
294
295 if (MF_DBGLEVEL >= MF_DBG_EXTENDED) {
296 Dbprintf("enc_B: %02x %02x %02x %02x %02x %02x %02x %02x",
297 enc_random_b[0],enc_random_b[1],enc_random_b[2],enc_random_b[3],enc_random_b[4],enc_random_b[5],enc_random_b[6],enc_random_b[7]);
298
299 Dbprintf(" B: %02x %02x %02x %02x %02x %02x %02x %02x",
300 random_b[0],random_b[1],random_b[2],random_b[3],random_b[4],random_b[5],random_b[6],random_b[7]);
301
302 Dbprintf("rnd_ab: %02x %02x %02x %02x %02x %02x %02x %02x",
303 rnd_ab[0],rnd_ab[1],rnd_ab[2],rnd_ab[3],rnd_ab[4],rnd_ab[5],rnd_ab[6],rnd_ab[7]);
304
305 Dbprintf("rnd_ab: %02x %02x %02x %02x %02x %02x %02x %02x",
306 rnd_ab[8],rnd_ab[9],rnd_ab[10],rnd_ab[11],rnd_ab[12],rnd_ab[13],rnd_ab[14],rnd_ab[15] );
307 }
308
309 // encrypt out, in, length, key, iv
310 tdes_2key_enc(rnd_ab, rnd_ab, sizeof(rnd_ab), key, enc_random_b);
311
312 len = mifare_sendcmd(0xAF, rnd_ab, sizeof(rnd_ab), resp, respPar, NULL);
313 if (len != 11) {
314 if (MF_DBGLEVEL >= MF_DBG_ERROR) Dbprintf("Cmd Error: %02x", resp[0]);
315 return 0;
316 }
317
318 uint8_t enc_resp[8] = { 0,0,0,0,0,0,0,0 };
319 uint8_t resp_random_a[8] = { 0,0,0,0,0,0,0,0 };
320 memcpy(enc_resp, resp+1, 8);
321
322 // decrypt out, in, length, key, iv
323 tdes_2key_dec(resp_random_a, enc_resp, 8, key, enc_random_b);
324 if ( memcmp(resp_random_a, random_a, 8) != 0 ) {
325 if (MF_DBGLEVEL >= MF_DBG_ERROR) Dbprintf("failed authentication");
326 return 0;
327 }
328
329 if (MF_DBGLEVEL >= MF_DBG_EXTENDED) {
330 Dbprintf("e_AB: %02x %02x %02x %02x %02x %02x %02x %02x",
331 rnd_ab[0],rnd_ab[1],rnd_ab[2],rnd_ab[3],
332 rnd_ab[4],rnd_ab[5],rnd_ab[6],rnd_ab[7]);
333
334 Dbprintf("e_AB: %02x %02x %02x %02x %02x %02x %02x %02x",
335 rnd_ab[8],rnd_ab[9],rnd_ab[10],rnd_ab[11],
336 rnd_ab[12],rnd_ab[13],rnd_ab[14],rnd_ab[15]);
337
338 Dbprintf("a: %02x %02x %02x %02x %02x %02x %02x %02x",
339 random_a[0],random_a[1],random_a[2],random_a[3],
340 random_a[4],random_a[5],random_a[6],random_a[7]);
341
342 Dbprintf("b: %02x %02x %02x %02x %02x %02x %02x %02x",
343 resp_random_a[0],resp_random_a[1],resp_random_a[2],resp_random_a[3],
344 resp_random_a[4],resp_random_a[5],resp_random_a[6],resp_random_a[7]);
345 }
346 return 1;
347 }
348
349 int mifare_ultra_readblock(uint8_t blockNo, uint8_t *blockData) {
350 uint16_t len = 0;
351 uint8_t bt[2] = {0x00, 0x00};
352 uint8_t receivedAnswer[MAX_FRAME_SIZE] = {0x00};
353 uint8_t receivedAnswerPar[MAX_PARITY_SIZE] = {0x00};
354
355 len = mifare_sendcmd_short(NULL, 1, 0x30, blockNo, receivedAnswer, receivedAnswerPar, NULL);
356 if (len == 1) {
357 if (MF_DBGLEVEL >= MF_DBG_ERROR) Dbprintf("Cmd Error: %02x", receivedAnswer[0]);
358 return 1;
359 }
360 if (len != 18) {
361 if (MF_DBGLEVEL >= MF_DBG_ERROR) Dbprintf("Cmd Error: card timeout. len: %x", len);
362 return 2;
363 }
364
365 memcpy(bt, receivedAnswer + 16, 2);
366 AppendCrc14443a(receivedAnswer, 16);
367 if (bt[0] != receivedAnswer[16] || bt[1] != receivedAnswer[17]) {
368 if (MF_DBGLEVEL >= MF_DBG_ERROR) Dbprintf("Cmd CRC response error.");
369 return 3;
370 }
371
372 memcpy(blockData, receivedAnswer, 14);
373 return 0;
374 }
375
376 int mifare_classic_writeblock(struct Crypto1State *pcs, uint32_t uid, uint8_t blockNo, uint8_t *blockData) {
377 // variables
378 uint16_t len = 0;
379 uint32_t pos = 0;
380 uint8_t par[3] = {0x00, 0x00, 0x00}; // enough for 18 Bytes to send
381 byte_t res = 0;
382
383 uint8_t d_block[18], d_block_enc[18];
384 uint8_t receivedAnswer[MAX_MIFARE_FRAME_SIZE] = {0x00};
385 uint8_t receivedAnswerPar[MAX_MIFARE_PARITY_SIZE] = {0x00};
386
387 // command MIFARE_CLASSIC_WRITEBLOCK
388 len = mifare_sendcmd_short(pcs, 1, 0xA0, blockNo, receivedAnswer, receivedAnswerPar, NULL);
389
390 if ((len != 1) || (receivedAnswer[0] != 0x0A)) { // 0x0a - ACK
391 if (MF_DBGLEVEL >= 1) Dbprintf("Cmd Error: %02x", receivedAnswer[0]);
392 return 1;
393 }
394
395 memcpy(d_block, blockData, 16);
396 AppendCrc14443a(d_block, 16);
397
398 // crypto
399 for (pos = 0; pos < 18; pos++) {
400 d_block_enc[pos] = crypto1_byte(pcs, 0x00, 0) ^ d_block[pos];
401 par[pos>>3] |= (((filter(pcs->odd) ^ oddparity8(d_block[pos])) & 0x01) << (7 - (pos&0x0007)));
402 }
403
404 ReaderTransmitPar(d_block_enc, sizeof(d_block_enc), par, NULL);
405
406 // Receive the response
407 len = ReaderReceive(receivedAnswer, receivedAnswerPar);
408
409 res = 0;
410 res |= (crypto1_bit(pcs, 0, 0) ^ BIT(receivedAnswer[0], 0)) << 0;
411 res |= (crypto1_bit(pcs, 0, 0) ^ BIT(receivedAnswer[0], 1)) << 1;
412 res |= (crypto1_bit(pcs, 0, 0) ^ BIT(receivedAnswer[0], 2)) << 2;
413 res |= (crypto1_bit(pcs, 0, 0) ^ BIT(receivedAnswer[0], 3)) << 3;
414
415 if ((len != 1) || (res != 0x0A)) {
416 if (MF_DBGLEVEL >= 1) Dbprintf("Cmd send data2 Error: %02x", res);
417 return 2;
418 }
419 return 0;
420 }
421
422 /* // command not needed, but left for future testing
423 int mifare_ultra_writeblock_compat(uint8_t blockNo, uint8_t *blockData) {
424 uint16_t len;
425 uint8_t par[3] = {0}; // enough for 18 parity bits
426 uint8_t d_block[18] = {0x00};
427 uint8_t receivedAnswer[MAX_FRAME_SIZE];
428 uint8_t receivedAnswerPar[MAX_PARITY_SIZE];
429
430 len = mifare_sendcmd_short(NULL, true, 0xA0, blockNo, receivedAnswer, receivedAnswerPar, NULL);
431
432 if ((len != 1) || (receivedAnswer[0] != 0x0A)) { // 0x0a - ACK
433 if (MF_DBGLEVEL >= MF_DBG_ERROR)
434 Dbprintf("Cmd Addr Error: %02x", receivedAnswer[0]);
435 return 1;
436 }
437
438 memcpy(d_block, blockData, 16);
439 AppendCrc14443a(d_block, 16);
440
441 ReaderTransmitPar(d_block, sizeof(d_block), par, NULL);
442
443 len = ReaderReceive(receivedAnswer, receivedAnswerPar);
444
445 if ((len != 1) || (receivedAnswer[0] != 0x0A)) { // 0x0a - ACK
446 if (MF_DBGLEVEL >= MF_DBG_ERROR)
447 Dbprintf("Cmd Data Error: %02x %d", receivedAnswer[0],len);
448 return 2;
449 }
450 return 0;
451 }
452 */
453
454 int mifare_ultra_writeblock(uint8_t blockNo, uint8_t *blockData) {
455 uint16_t len = 0;
456 uint8_t block[5] = {blockNo, 0x00, 0x00, 0x00, 0x00 };
457 uint8_t receivedAnswer[MAX_MIFARE_FRAME_SIZE] = {0x00};
458 uint8_t receivedAnswerPar[MAX_MIFARE_PARITY_SIZE] = {0x00};
459
460 // command MIFARE_CLASSIC_WRITEBLOCK
461 memcpy(block+1, blockData, 4);
462
463 len = mifare_sendcmd( MIFARE_ULC_WRITE, block, sizeof(block), receivedAnswer, receivedAnswerPar, NULL);
464
465 if (receivedAnswer[0] != 0x0A) { // 0x0a - ACK
466 if (MF_DBGLEVEL >= MF_DBG_ERROR)
467 Dbprintf("Cmd Send Error: %02x %d", receivedAnswer[0],len);
468 return 1;
469 }
470 return 0;
471 }
472 int mifare_classic_halt_ex(struct Crypto1State *pcs) {
473 uint8_t receivedAnswer[4] = {0x00, 0x00, 0x00, 0x00};
474 uint16_t len = mifare_sendcmd_short(pcs, (pcs == NULL) ? CRYPT_NONE : CRYPT_ALL, 0x50, 0x00, receivedAnswer, NULL, NULL);
475 if (len != 0) {
476 if (MF_DBGLEVEL >= MF_DBG_EXTENDED) Dbprintf("halt warning. response len: %x", len);
477 return 1;
478 }
479 return 0;
480 }
481 int mifare_classic_halt(struct Crypto1State *pcs, uint32_t uid) {
482 return mifare_classic_halt_ex(pcs);
483 }
484
485 int mifare_ultra_halt() {
486 uint16_t len = 0;
487 uint8_t receivedAnswer[4] = {0x00, 0x00, 0x00, 0x00};
488 len = mifare_sendcmd_short(NULL, CRYPT_NONE, 0x50, 0x00, receivedAnswer, NULL, NULL);
489 if (len != 0) {
490 if (MF_DBGLEVEL >= MF_DBG_ERROR) Dbprintf("halt error. response len: %x", len);
491 return 1;
492 }
493 return 0;
494 }
495
496
497 // Mifare Memory Structure: up to 32 Sectors with 4 blocks each (1k and 2k cards),
498 // plus evtl. 8 sectors with 16 blocks each (4k cards)
499 uint8_t NumBlocksPerSector(uint8_t sectorNo) {
500 return (sectorNo < 32) ? 4 : 16;
501 }
502
503 uint8_t FirstBlockOfSector(uint8_t sectorNo) {
504 if (sectorNo < 32)
505 return sectorNo * 4;
506 else
507 return 32*4 + (sectorNo - 32) * 16;
508
509 }
510
511 // work with emulator memory
512 void emlSetMem(uint8_t *data, int blockNum, int blocksCount) {
513 emlSetMem_xt(data, blockNum, blocksCount, 16);
514 }
515
516 void emlSetMem_xt(uint8_t *data, int blockNum, int blocksCount, int blockBtWidth) {
517 uint8_t* emCARD = BigBuf_get_EM_addr();
518 memcpy(emCARD + blockNum * blockBtWidth, data, blocksCount * blockBtWidth);
519 }
520
521 void emlGetMem(uint8_t *data, int blockNum, int blocksCount) {
522 uint8_t* emCARD = BigBuf_get_EM_addr();
523 memcpy(data, emCARD + blockNum * 16, blocksCount * 16);
524 }
525
526 void emlGetMemBt(uint8_t *data, int bytePtr, int byteCount) {
527 uint8_t* emCARD = BigBuf_get_EM_addr();
528 memcpy(data, emCARD + bytePtr, byteCount);
529 }
530
531 int emlCheckValBl(int blockNum) {
532 uint8_t* emCARD = BigBuf_get_EM_addr();
533 uint8_t* data = emCARD + blockNum * 16;
534
535 if ((data[0] != (data[4] ^ 0xff)) || (data[0] != data[8]) ||
536 (data[1] != (data[5] ^ 0xff)) || (data[1] != data[9]) ||
537 (data[2] != (data[6] ^ 0xff)) || (data[2] != data[10]) ||
538 (data[3] != (data[7] ^ 0xff)) || (data[3] != data[11]) ||
539 (data[12] != (data[13] ^ 0xff)) || (data[12] != data[14]) ||
540 (data[12] != (data[15] ^ 0xff))
541 )
542 return 1;
543 return 0;
544 }
545
546 int emlGetValBl(uint32_t *blReg, uint8_t *blBlock, int blockNum) {
547 uint8_t* emCARD = BigBuf_get_EM_addr();
548 uint8_t* data = emCARD + blockNum * 16;
549
550 if (emlCheckValBl(blockNum))
551 return 1;
552
553 memcpy(blReg, data, 4);
554 *blBlock = data[12];
555 return 0;
556 }
557
558 int emlSetValBl(uint32_t blReg, uint8_t blBlock, int blockNum) {
559 uint8_t* emCARD = BigBuf_get_EM_addr();
560 uint8_t* data = emCARD + blockNum * 16;
561
562 memcpy(data + 0, &blReg, 4);
563 memcpy(data + 8, &blReg, 4);
564 blReg = blReg ^ 0xffffffff;
565 memcpy(data + 4, &blReg, 4);
566
567 data[12] = blBlock;
568 data[13] = blBlock ^ 0xff;
569 data[14] = blBlock;
570 data[15] = blBlock ^ 0xff;
571
572 return 0;
573 }
574
575 uint64_t emlGetKey(int sectorNum, int keyType) {
576 uint8_t key[6] = {0x00};
577 uint8_t* emCARD = BigBuf_get_EM_addr();
578
579 memcpy(key, emCARD + 16 * (FirstBlockOfSector(sectorNum) + NumBlocksPerSector(sectorNum) - 1) + keyType * 10, 6);
580 return bytes_to_num(key, 6);
581 }
582
583 void emlClearMem(void) {
584 int b;
585
586 const uint8_t trailer[] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x07, 0x80, 0x69, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
587 const uint8_t uid[] = {0xe6, 0x84, 0x87, 0xf3, 0x16, 0x88, 0x04, 0x00, 0x46, 0x8e, 0x45, 0x55, 0x4d, 0x70, 0x41, 0x04};
588 uint8_t* emCARD = BigBuf_get_EM_addr();
589
590 memset(emCARD, 0, CARD_MEMORY_SIZE);
591
592 // fill sectors trailer data
593 for(b = 3; b < 256; b<127?(b+=4):(b+=16))
594 emlSetMem((uint8_t *)trailer, b , 1);
595
596 // uid
597 emlSetMem((uint8_t *)uid, 0, 1);
598 return;
599 }
600
601
602 // Mifare desfire commands
603 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)
604 {
605 uint8_t dcmd[5] = {cmd, data[0], data[1], 0x00, 0x00};
606 AppendCrc14443a(dcmd, 3);
607
608 ReaderTransmit(dcmd, sizeof(dcmd), NULL);
609 int len = ReaderReceive(answer, answer_parity);
610 if(!len) {
611 if (MF_DBGLEVEL >= MF_DBG_ERROR)
612 Dbprintf("Authentication failed. Card timeout.");
613 return 1;
614 }
615 return len;
616 }
617
618 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)
619 {
620 uint8_t dcmd[20] = {0x00};
621 dcmd[0] = cmd;
622 memcpy(dcmd+1,data,17);
623 AppendCrc14443a(dcmd, 18);
624
625 ReaderTransmit(dcmd, sizeof(dcmd), NULL);
626 int len = ReaderReceive(answer, answer_parity);
627 if(!len){
628 if (MF_DBGLEVEL >= MF_DBG_ERROR)
629 Dbprintf("Authentication failed. Card timeout.");
630 return 1;
631 }
632 return len;
633 }
634
635 int mifare_desfire_des_auth1(uint32_t uid, uint8_t *blockData){
636
637 int len;
638 // load key, keynumber
639 uint8_t data[2]={0x0a, 0x00};
640 uint8_t receivedAnswer[MAX_FRAME_SIZE] = {0x00};
641 uint8_t receivedAnswerPar[MAX_PARITY_SIZE] = {0x00};
642
643 len = mifare_sendcmd_special(NULL, 1, 0x02, data, receivedAnswer,receivedAnswerPar,NULL);
644 if (len == 1) {
645 if (MF_DBGLEVEL >= MF_DBG_ERROR)
646 Dbprintf("Cmd Error: %02x", receivedAnswer[0]);
647 return 1;
648 }
649
650 if (len == 12) {
651 if (MF_DBGLEVEL >= MF_DBG_EXTENDED) {
652 Dbprintf("Auth1 Resp: %02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",
653 receivedAnswer[0],receivedAnswer[1],receivedAnswer[2],receivedAnswer[3],receivedAnswer[4],
654 receivedAnswer[5],receivedAnswer[6],receivedAnswer[7],receivedAnswer[8],receivedAnswer[9],
655 receivedAnswer[10],receivedAnswer[11]);
656 }
657 memcpy(blockData, receivedAnswer, 12);
658 return 0;
659 }
660 return 1;
661 }
662
663 int mifare_desfire_des_auth2(uint32_t uid, uint8_t *key, uint8_t *blockData){
664
665 int len;
666 uint8_t data[17] = {0x00};
667 data[0] = 0xAF;
668 memcpy(data+1,key,16);
669
670 uint8_t receivedAnswer[MAX_FRAME_SIZE] = {0x00};
671 uint8_t receivedAnswerPar[MAX_PARITY_SIZE] = {0x00};
672
673 len = mifare_sendcmd_special2(NULL, 1, 0x03, data, receivedAnswer, receivedAnswerPar ,NULL);
674
675 if ((receivedAnswer[0] == 0x03) && (receivedAnswer[1] == 0xae)) {
676 if (MF_DBGLEVEL >= MF_DBG_ERROR)
677 Dbprintf("Auth Error: %02x %02x", receivedAnswer[0], receivedAnswer[1]);
678 return 1;
679 }
680
681 if (len == 12){
682 if (MF_DBGLEVEL >= MF_DBG_EXTENDED) {
683 Dbprintf("Auth2 Resp: %02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",
684 receivedAnswer[0],receivedAnswer[1],receivedAnswer[2],receivedAnswer[3],receivedAnswer[4],
685 receivedAnswer[5],receivedAnswer[6],receivedAnswer[7],receivedAnswer[8],receivedAnswer[9],
686 receivedAnswer[10],receivedAnswer[11]);
687 }
688 memcpy(blockData, receivedAnswer, 12);
689 return 0;
690 }
691 return 1;
692 }
Impressum, Datenschutz