]> cvs.zerfleddert.de Git - proxmark3-svn/blob - armsrc/mifareutil.c
TLV decoding works
[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 <string.h>
13 #include "mifareutil.h"
14 #include "proxmark3.h"
15 #include "apps.h"
16 #include "util.h"
17 #include "parity.h"
18
19 #include "iso14443crc.h"
20 #include "iso14443a.h"
21 #include "crapto1/crapto1.h"
22 #include "polarssl/des.h"
23
24 int MF_DBGLEVEL = MF_DBG_ALL;
25
26 // crypto1 helpers
27 void mf_crypto1_decryptEx(struct Crypto1State *pcs, uint8_t *data_in, int len, uint8_t *data_out){
28 uint8_t bt = 0;
29 int i;
30
31 if (len != 1) {
32 for (i = 0; i < len; i++)
33 data_out[i] = crypto1_byte(pcs, 0x00, 0) ^ data_in[i];
34 } else {
35 bt = 0;
36 for (i = 0; i < 4; i++)
37 bt |= (crypto1_bit(pcs, 0, 0) ^ BIT(data_in[0], i)) << i;
38
39 data_out[0] = bt;
40 }
41 return;
42 }
43
44 void mf_crypto1_decrypt(struct Crypto1State *pcs, uint8_t *data, int len){
45 mf_crypto1_decryptEx(pcs, data, len, data);
46 }
47
48 void mf_crypto1_encrypt(struct Crypto1State *pcs, uint8_t *data, uint16_t len, uint8_t *par) {
49 uint8_t bt = 0;
50 int i;
51 par[0] = 0;
52
53 for (i = 0; i < len; i++) {
54 bt = data[i];
55 data[i] = crypto1_byte(pcs, 0x00, 0) ^ data[i];
56 if((i&0x0007) == 0)
57 par[i>>3] = 0;
58 par[i>>3] |= (((filter(pcs->odd) ^ oddparity8(bt)) & 0x01)<<(7-(i&0x0007)));
59 }
60 return;
61 }
62
63 uint8_t mf_crypto1_encrypt4bit(struct Crypto1State *pcs, uint8_t data) {
64 uint8_t bt = 0;
65 int i;
66
67 for (i = 0; i < 4; i++)
68 bt |= (crypto1_bit(pcs, 0, 0) ^ BIT(data, i)) << i;
69
70 return bt;
71 }
72
73 // send X byte basic commands
74 int mifare_sendcmd(uint8_t cmd, uint8_t* data, uint8_t data_size, uint8_t* answer, uint8_t *answer_parity, uint32_t *timing)
75 {
76 uint8_t dcmd[data_size+3];
77 dcmd[0] = cmd;
78 memcpy(dcmd+1,data,data_size);
79 AppendCrc14443a(dcmd, data_size+1);
80 ReaderTransmit(dcmd, sizeof(dcmd), timing);
81 int len = ReaderReceive(answer, answer_parity);
82 if(!len) {
83 if (MF_DBGLEVEL >= MF_DBG_ERROR) Dbprintf("%02X Cmd failed. Card timeout.", cmd);
84 len = ReaderReceive(answer,answer_parity);
85 //return 0;
86 }
87 return len;
88 }
89
90 // send 2 byte commands
91 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)
92 {
93 uint8_t dcmd[4], ecmd[4];
94 uint16_t pos, res;
95 uint8_t par[1]; // 1 Byte parity is enough here
96 dcmd[0] = cmd;
97 dcmd[1] = data;
98 AppendCrc14443a(dcmd, 2);
99
100 memcpy(ecmd, dcmd, sizeof(dcmd));
101
102 if (crypted) {
103 par[0] = 0;
104 for (pos = 0; pos < 4; pos++)
105 {
106 ecmd[pos] = crypto1_byte(pcs, 0x00, 0) ^ dcmd[pos];
107 par[0] |= (((filter(pcs->odd) ^ oddparity8(dcmd[pos])) & 0x01) << (7-pos));
108 }
109
110 ReaderTransmitPar(ecmd, sizeof(ecmd), par, timing);
111
112 } else {
113 ReaderTransmit(dcmd, sizeof(dcmd), timing);
114 }
115
116 int len = ReaderReceive(answer, par);
117
118 if (answer_parity) *answer_parity = par[0];
119
120 if (crypted == CRYPT_ALL) {
121 if (len == 1) {
122 res = 0;
123 for (pos = 0; pos < 4; pos++)
124 res |= (crypto1_bit(pcs, 0, 0) ^ BIT(answer[0], pos)) << pos;
125
126 answer[0] = res;
127
128 } else {
129 for (pos = 0; pos < len; pos++)
130 {
131 answer[pos] = crypto1_byte(pcs, 0x00, 0) ^ answer[pos];
132 }
133 }
134 }
135
136 return len;
137 }
138
139 // mifare classic commands
140 int mifare_classic_auth(struct Crypto1State *pcs, uint32_t uid, uint8_t blockNo, uint8_t keyType, uint64_t ui64Key, uint8_t isNested)
141 {
142 return mifare_classic_authex(pcs, uid, blockNo, keyType, ui64Key, isNested, NULL, NULL);
143 }
144
145 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)
146 {
147 // variables
148 int len;
149 uint32_t pos;
150 uint8_t tmp4[4];
151 uint8_t par[1] = {0x00};
152 byte_t nr[4];
153 uint32_t nt, ntpp; // Supplied tag nonce
154
155 uint8_t mf_nr_ar[] = { 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 };
156 uint8_t receivedAnswer[MAX_MIFARE_FRAME_SIZE];
157 uint8_t receivedAnswerPar[MAX_MIFARE_PARITY_SIZE];
158
159 // Transmit MIFARE_CLASSIC_AUTH
160 len = mifare_sendcmd_short(pcs, isNested, 0x60 + (keyType & 0x01), blockNo, receivedAnswer, receivedAnswerPar, timing);
161 if (MF_DBGLEVEL >= 4) Dbprintf("rand tag nonce len: %x", len);
162 if (len != 4) return 1;
163
164 // "random" reader nonce:
165 nr[0] = 0x55;
166 nr[1] = 0x41;
167 nr[2] = 0x49;
168 nr[3] = 0x92;
169
170 // Save the tag nonce (nt)
171 nt = bytes_to_num(receivedAnswer, 4);
172
173 // ----------------------------- crypto1 create
174 if (isNested)
175 crypto1_destroy(pcs);
176
177 // Init cipher with key
178 crypto1_create(pcs, ui64Key);
179
180 if (isNested == AUTH_NESTED) {
181 // decrypt nt with help of new key
182 nt = crypto1_word(pcs, nt ^ uid, 1) ^ nt;
183 } else {
184 // Load (plain) uid^nt into the cipher
185 crypto1_word(pcs, nt ^ uid, 0);
186 }
187
188 // some statistic
189 if (!ntptr && (MF_DBGLEVEL >= 3))
190 Dbprintf("auth uid: %08x nt: %08x", uid, nt);
191
192 // save Nt
193 if (ntptr)
194 *ntptr = nt;
195
196 // Generate (encrypted) nr+parity by loading it into the cipher (Nr)
197 par[0] = 0;
198 for (pos = 0; pos < 4; pos++)
199 {
200 mf_nr_ar[pos] = crypto1_byte(pcs, nr[pos], 0) ^ nr[pos];
201 par[0] |= (((filter(pcs->odd) ^ oddparity8(nr[pos])) & 0x01) << (7-pos));
202 }
203
204 // Skip 32 bits in pseudo random generator
205 nt = prng_successor(nt,32);
206
207 // ar+parity
208 for (pos = 4; pos < 8; pos++)
209 {
210 nt = prng_successor(nt,8);
211 mf_nr_ar[pos] = crypto1_byte(pcs,0x00,0) ^ (nt & 0xff);
212 par[0] |= (((filter(pcs->odd) ^ oddparity8(nt)) & 0x01) << (7-pos));
213 }
214
215 // Transmit reader nonce and reader answer
216 ReaderTransmitPar(mf_nr_ar, sizeof(mf_nr_ar), par, NULL);
217
218 // Receive 4 byte tag answer
219 len = ReaderReceive(receivedAnswer, receivedAnswerPar);
220 if (!len)
221 {
222 if (MF_DBGLEVEL >= 1) Dbprintf("Authentication failed. Card timeout.");
223 return 2;
224 }
225
226 memcpy(tmp4, receivedAnswer, 4);
227 ntpp = prng_successor(nt, 32) ^ crypto1_word(pcs, 0,0);
228
229 if (ntpp != bytes_to_num(tmp4, 4)) {
230 if (MF_DBGLEVEL >= 1) Dbprintf("Authentication failed. Error card response.");
231 return 3;
232 }
233
234 return 0;
235 }
236
237 int mifare_classic_readblock(struct Crypto1State *pcs, uint32_t uid, uint8_t blockNo, uint8_t *blockData)
238 {
239 // variables
240 int len;
241 uint8_t bt[2];
242
243 uint8_t receivedAnswer[MAX_MIFARE_FRAME_SIZE];
244 uint8_t receivedAnswerPar[MAX_MIFARE_PARITY_SIZE];
245
246 // command MIFARE_CLASSIC_READBLOCK
247 len = mifare_sendcmd_short(pcs, 1, 0x30, blockNo, receivedAnswer, receivedAnswerPar, NULL);
248 if (len == 1) {
249 if (MF_DBGLEVEL >= 1) Dbprintf("Cmd Error: %02x", receivedAnswer[0]);
250 return 1;
251 }
252 if (len != 18) {
253 if (MF_DBGLEVEL >= 1) Dbprintf("Cmd Error: card timeout. len: %x", len);
254 return 2;
255 }
256
257 memcpy(bt, receivedAnswer + 16, 2);
258 AppendCrc14443a(receivedAnswer, 16);
259 if (bt[0] != receivedAnswer[16] || bt[1] != receivedAnswer[17]) {
260 if (MF_DBGLEVEL >= 1) Dbprintf("Cmd CRC response error.");
261 return 3;
262 }
263
264 memcpy(blockData, receivedAnswer, 16);
265 return 0;
266 }
267
268 // mifare ultralight commands
269 int mifare_ul_ev1_auth(uint8_t *keybytes, uint8_t *pack){
270
271 uint16_t len;
272 uint8_t resp[4];
273 uint8_t respPar[1];
274 uint8_t key[4] = {0x00};
275 memcpy(key, keybytes, 4);
276
277 if (MF_DBGLEVEL >= MF_DBG_EXTENDED)
278 Dbprintf("EV1 Auth : %02x%02x%02x%02x", key[0], key[1], key[2], key[3]);
279 len = mifare_sendcmd(0x1B, key, sizeof(key), resp, respPar, NULL);
280 //len = mifare_sendcmd_short_mfuev1auth(NULL, 0, 0x1B, key, resp, respPar, NULL);
281 if (len != 4) {
282 if (MF_DBGLEVEL >= MF_DBG_ERROR) Dbprintf("Cmd Error: %02x %u", resp[0], len);
283 return 0;
284 }
285
286 if (MF_DBGLEVEL >= MF_DBG_EXTENDED)
287 Dbprintf("Auth Resp: %02x%02x%02x%02x", resp[0],resp[1],resp[2],resp[3]);
288
289 memcpy(pack, resp, 4);
290 return 1;
291 }
292
293 int mifare_ultra_auth(uint8_t *keybytes){
294
295 /// 3des2k
296
297 des3_context ctx = { 0x00 };
298 uint8_t random_a[8] = {1,1,1,1,1,1,1,1};
299 uint8_t random_b[8] = {0x00};
300 uint8_t enc_random_b[8] = {0x00};
301 uint8_t rnd_ab[16] = {0x00};
302 uint8_t IV[8] = {0x00};
303 uint8_t key[16] = {0x00};
304 memcpy(key, keybytes, 16);
305
306 uint16_t len;
307 uint8_t resp[19] = {0x00};
308 uint8_t respPar[3] = {0,0,0};
309
310 // REQUEST AUTHENTICATION
311 len = mifare_sendcmd_short(NULL, 1, 0x1A, 0x00, resp, respPar ,NULL);
312 if (len != 11) {
313 if (MF_DBGLEVEL >= MF_DBG_ERROR) Dbprintf("Cmd Error: %02x", resp[0]);
314 return 0;
315 }
316
317 // tag nonce.
318 memcpy(enc_random_b,resp+1,8);
319
320 // decrypt nonce.
321 // tdes_2key_dec(random_b, enc_random_b, sizeof(random_b), key, IV );
322 des3_set2key_dec(&ctx, key);
323 des3_crypt_cbc(&ctx // des3_context
324 , DES_DECRYPT // int mode
325 , sizeof(random_b) // length
326 , IV // iv[8]
327 , enc_random_b // input
328 , random_b // output
329 );
330
331 rol(random_b,8);
332 memcpy(rnd_ab ,random_a,8);
333 memcpy(rnd_ab+8,random_b,8);
334
335 if (MF_DBGLEVEL >= MF_DBG_EXTENDED) {
336 Dbprintf("enc_B: %02x %02x %02x %02x %02x %02x %02x %02x",
337 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]);
338
339 Dbprintf(" B: %02x %02x %02x %02x %02x %02x %02x %02x",
340 random_b[0],random_b[1],random_b[2],random_b[3],random_b[4],random_b[5],random_b[6],random_b[7]);
341
342 Dbprintf("rnd_ab: %02x %02x %02x %02x %02x %02x %02x %02x",
343 rnd_ab[0],rnd_ab[1],rnd_ab[2],rnd_ab[3],rnd_ab[4],rnd_ab[5],rnd_ab[6],rnd_ab[7]);
344
345 Dbprintf("rnd_ab: %02x %02x %02x %02x %02x %02x %02x %02x",
346 rnd_ab[8],rnd_ab[9],rnd_ab[10],rnd_ab[11],rnd_ab[12],rnd_ab[13],rnd_ab[14],rnd_ab[15] );
347 }
348
349 // encrypt out, in, length, key, iv
350 //tdes_2key_enc(rnd_ab, rnd_ab, sizeof(rnd_ab), key, enc_random_b);
351 des3_set2key_enc(&ctx, key);
352 des3_crypt_cbc(&ctx // des3_context
353 , DES_ENCRYPT // int mode
354 , sizeof(rnd_ab) // length
355 , enc_random_b // iv[8]
356 , rnd_ab // input
357 , rnd_ab // output
358 );
359
360 //len = mifare_sendcmd_short_mfucauth(NULL, 1, 0xAF, rnd_ab, resp, respPar, NULL);
361 len = mifare_sendcmd(0xAF, rnd_ab, sizeof(rnd_ab), resp, respPar, NULL);
362 if (len != 11) {
363 if (MF_DBGLEVEL >= MF_DBG_ERROR) Dbprintf("Cmd Error: %02x", resp[0]);
364 return 0;
365 }
366
367 uint8_t enc_resp[8] = { 0,0,0,0,0,0,0,0 };
368 uint8_t resp_random_a[8] = { 0,0,0,0,0,0,0,0 };
369 memcpy(enc_resp, resp+1, 8);
370
371 // decrypt out, in, length, key, iv
372 // tdes_2key_dec(resp_random_a, enc_resp, 8, key, enc_random_b);
373 des3_set2key_dec(&ctx, key);
374 des3_crypt_cbc(&ctx // des3_context
375 , DES_DECRYPT // int mode
376 , 8 // length
377 , enc_random_b // iv[8]
378 , enc_resp // input
379 , resp_random_a // output
380 );
381 if ( memcmp(resp_random_a, random_a, 8) != 0 ) {
382 if (MF_DBGLEVEL >= MF_DBG_ERROR) Dbprintf("failed authentication");
383 return 0;
384 }
385
386 if (MF_DBGLEVEL >= MF_DBG_EXTENDED) {
387 Dbprintf("e_AB: %02x %02x %02x %02x %02x %02x %02x %02x",
388 rnd_ab[0],rnd_ab[1],rnd_ab[2],rnd_ab[3],
389 rnd_ab[4],rnd_ab[5],rnd_ab[6],rnd_ab[7]);
390
391 Dbprintf("e_AB: %02x %02x %02x %02x %02x %02x %02x %02x",
392 rnd_ab[8],rnd_ab[9],rnd_ab[10],rnd_ab[11],
393 rnd_ab[12],rnd_ab[13],rnd_ab[14],rnd_ab[15]);
394
395 Dbprintf("a: %02x %02x %02x %02x %02x %02x %02x %02x",
396 random_a[0],random_a[1],random_a[2],random_a[3],
397 random_a[4],random_a[5],random_a[6],random_a[7]);
398
399 Dbprintf("b: %02x %02x %02x %02x %02x %02x %02x %02x",
400 resp_random_a[0],resp_random_a[1],resp_random_a[2],resp_random_a[3],
401 resp_random_a[4],resp_random_a[5],resp_random_a[6],resp_random_a[7]);
402 }
403 return 1;
404 }
405
406 int mifare_ultra_readblock(uint8_t blockNo, uint8_t *blockData)
407 {
408 uint16_t len;
409 uint8_t bt[2];
410 uint8_t receivedAnswer[MAX_FRAME_SIZE];
411 uint8_t receivedAnswerPar[MAX_PARITY_SIZE];
412
413
414 len = mifare_sendcmd_short(NULL, 1, 0x30, blockNo, receivedAnswer, receivedAnswerPar, NULL);
415 if (len == 1) {
416 if (MF_DBGLEVEL >= MF_DBG_ERROR) Dbprintf("Cmd Error: %02x", receivedAnswer[0]);
417 return 1;
418 }
419 if (len != 18) {
420 if (MF_DBGLEVEL >= MF_DBG_ERROR) Dbprintf("Cmd Error: card timeout. len: %x", len);
421 return 2;
422 }
423
424 memcpy(bt, receivedAnswer + 16, 2);
425 AppendCrc14443a(receivedAnswer, 16);
426 if (bt[0] != receivedAnswer[16] || bt[1] != receivedAnswer[17]) {
427 if (MF_DBGLEVEL >= MF_DBG_ERROR) Dbprintf("Cmd CRC response error.");
428 return 3;
429 }
430
431 memcpy(blockData, receivedAnswer, 14);
432 return 0;
433 }
434
435 int mifare_classic_writeblock(struct Crypto1State *pcs, uint32_t uid, uint8_t blockNo, uint8_t *blockData)
436 {
437 // variables
438 uint16_t len, i;
439 uint32_t pos;
440 uint8_t par[3] = {0}; // enough for 18 Bytes to send
441 byte_t res;
442
443 uint8_t d_block[18], d_block_enc[18];
444 uint8_t receivedAnswer[MAX_MIFARE_FRAME_SIZE];
445 uint8_t receivedAnswerPar[MAX_MIFARE_PARITY_SIZE];
446
447 // command MIFARE_CLASSIC_WRITEBLOCK
448 len = mifare_sendcmd_short(pcs, 1, 0xA0, blockNo, receivedAnswer, receivedAnswerPar, NULL);
449
450 if ((len != 1) || (receivedAnswer[0] != 0x0A)) { // 0x0a - ACK
451 if (MF_DBGLEVEL >= 1) Dbprintf("Cmd Error: %02x", receivedAnswer[0]);
452 return 1;
453 }
454
455 memcpy(d_block, blockData, 16);
456 AppendCrc14443a(d_block, 16);
457
458 // crypto
459 for (pos = 0; pos < 18; pos++)
460 {
461 d_block_enc[pos] = crypto1_byte(pcs, 0x00, 0) ^ d_block[pos];
462 par[pos>>3] |= (((filter(pcs->odd) ^ oddparity8(d_block[pos])) & 0x01) << (7 - (pos&0x0007)));
463 }
464
465 ReaderTransmitPar(d_block_enc, sizeof(d_block_enc), par, NULL);
466
467 // Receive the response
468 len = ReaderReceive(receivedAnswer, receivedAnswerPar);
469
470 res = 0;
471 for (i = 0; i < 4; i++)
472 res |= (crypto1_bit(pcs, 0, 0) ^ BIT(receivedAnswer[0], i)) << i;
473
474 if ((len != 1) || (res != 0x0A)) {
475 if (MF_DBGLEVEL >= 1) Dbprintf("Cmd send data2 Error: %02x", res);
476 return 2;
477 }
478
479 return 0;
480 }
481
482 /* // command not needed, but left for future testing
483 int mifare_ultra_writeblock_compat(uint8_t blockNo, uint8_t *blockData)
484 {
485 uint16_t len;
486 uint8_t par[3] = {0}; // enough for 18 parity bits
487 uint8_t d_block[18] = {0x00};
488 uint8_t receivedAnswer[MAX_FRAME_SIZE];
489 uint8_t receivedAnswerPar[MAX_PARITY_SIZE];
490
491 len = mifare_sendcmd_short(NULL, true, 0xA0, blockNo, receivedAnswer, receivedAnswerPar, NULL);
492
493 if ((len != 1) || (receivedAnswer[0] != 0x0A)) { // 0x0a - ACK
494 if (MF_DBGLEVEL >= MF_DBG_ERROR)
495 Dbprintf("Cmd Addr Error: %02x", receivedAnswer[0]);
496 return 1;
497 }
498
499 memcpy(d_block, blockData, 16);
500 AppendCrc14443a(d_block, 16);
501
502 ReaderTransmitPar(d_block, sizeof(d_block), par, NULL);
503
504 len = ReaderReceive(receivedAnswer, receivedAnswerPar);
505
506 if ((len != 1) || (receivedAnswer[0] != 0x0A)) { // 0x0a - ACK
507 if (MF_DBGLEVEL >= MF_DBG_ERROR)
508 Dbprintf("Cmd Data Error: %02x %d", receivedAnswer[0],len);
509 return 2;
510 }
511 return 0;
512 }
513 */
514
515 int mifare_ultra_writeblock(uint8_t blockNo, uint8_t *blockData)
516 {
517 uint16_t len;
518 uint8_t d_block[5] = {0x00};
519 uint8_t receivedAnswer[MAX_MIFARE_FRAME_SIZE];
520 uint8_t receivedAnswerPar[MAX_MIFARE_PARITY_SIZE];
521
522 // command MIFARE_CLASSIC_WRITEBLOCK
523 d_block[0]= blockNo;
524 memcpy(d_block+1,blockData,4);
525 //AppendCrc14443a(d_block, 6);
526
527 len = mifare_sendcmd(0xA2, d_block, sizeof(d_block), receivedAnswer, receivedAnswerPar, NULL);
528
529 if (receivedAnswer[0] != 0x0A) { // 0x0a - ACK
530 if (MF_DBGLEVEL >= MF_DBG_ERROR)
531 Dbprintf("Cmd Send Error: %02x %d", receivedAnswer[0],len);
532 return 1;
533 }
534 return 0;
535 }
536
537 int mifare_classic_halt(struct Crypto1State *pcs, uint32_t uid)
538 {
539 uint16_t len;
540 uint8_t receivedAnswer[MAX_MIFARE_FRAME_SIZE];
541 uint8_t receivedAnswerPar[MAX_MIFARE_PARITY_SIZE];
542
543 len = mifare_sendcmd_short(pcs, pcs == NULL ? false:true, 0x50, 0x00, receivedAnswer, receivedAnswerPar, NULL);
544 if (len != 0) {
545 if (MF_DBGLEVEL >= MF_DBG_ERROR)
546 Dbprintf("halt error. response len: %x", len);
547 return 1;
548 }
549
550 return 0;
551 }
552
553 int mifare_ultra_halt()
554 {
555 uint16_t len;
556 uint8_t receivedAnswer[MAX_MIFARE_FRAME_SIZE];
557 uint8_t receivedAnswerPar[MAX_MIFARE_PARITY_SIZE];
558
559 len = mifare_sendcmd_short(NULL, true, 0x50, 0x00, receivedAnswer, receivedAnswerPar, NULL);
560 if (len != 0) {
561 if (MF_DBGLEVEL >= MF_DBG_ERROR)
562 Dbprintf("halt error. response len: %x", len);
563 return 1;
564 }
565 return 0;
566 }
567
568
569 // Mifare Memory Structure: up to 32 Sectors with 4 blocks each (1k and 2k cards),
570 // plus evtl. 8 sectors with 16 blocks each (4k cards)
571 uint8_t NumBlocksPerSector(uint8_t sectorNo)
572 {
573 if (sectorNo < 32)
574 return 4;
575 else
576 return 16;
577 }
578
579 uint8_t FirstBlockOfSector(uint8_t sectorNo)
580 {
581 if (sectorNo < 32)
582 return sectorNo * 4;
583 else
584 return 32*4 + (sectorNo - 32) * 16;
585
586 }
587
588
589 // work with emulator memory
590 void emlSetMem(uint8_t *data, int blockNum, int blocksCount) {
591 uint8_t* emCARD = BigBuf_get_EM_addr();
592 memcpy(emCARD + blockNum * 16, data, blocksCount * 16);
593 }
594
595 void emlGetMem(uint8_t *data, int blockNum, int blocksCount) {
596 uint8_t* emCARD = BigBuf_get_EM_addr();
597 memcpy(data, emCARD + blockNum * 16, blocksCount * 16);
598 }
599
600 void emlGetMemBt(uint8_t *data, int bytePtr, int byteCount) {
601 uint8_t* emCARD = BigBuf_get_EM_addr();
602 memcpy(data, emCARD + bytePtr, byteCount);
603 }
604
605 int emlCheckValBl(int blockNum) {
606 uint8_t* emCARD = BigBuf_get_EM_addr();
607 uint8_t* data = emCARD + blockNum * 16;
608
609 if ((data[0] != (data[4] ^ 0xff)) || (data[0] != data[8]) ||
610 (data[1] != (data[5] ^ 0xff)) || (data[1] != data[9]) ||
611 (data[2] != (data[6] ^ 0xff)) || (data[2] != data[10]) ||
612 (data[3] != (data[7] ^ 0xff)) || (data[3] != data[11]) ||
613 (data[12] != (data[13] ^ 0xff)) || (data[12] != data[14]) ||
614 (data[12] != (data[15] ^ 0xff))
615 )
616 return 1;
617 return 0;
618 }
619
620 int emlGetValBl(uint32_t *blReg, uint8_t *blBlock, int blockNum) {
621 uint8_t* emCARD = BigBuf_get_EM_addr();
622 uint8_t* data = emCARD + blockNum * 16;
623
624 if (emlCheckValBl(blockNum)) {
625 return 1;
626 }
627
628 memcpy(blReg, data, 4);
629 *blBlock = data[12];
630 return 0;
631 }
632
633 int emlSetValBl(uint32_t blReg, uint8_t blBlock, int blockNum) {
634 uint8_t* emCARD = BigBuf_get_EM_addr();
635 uint8_t* data = emCARD + blockNum * 16;
636
637 memcpy(data + 0, &blReg, 4);
638 memcpy(data + 8, &blReg, 4);
639 blReg = blReg ^ 0xffffffff;
640 memcpy(data + 4, &blReg, 4);
641
642 data[12] = blBlock;
643 data[13] = blBlock ^ 0xff;
644 data[14] = blBlock;
645 data[15] = blBlock ^ 0xff;
646
647 return 0;
648 }
649
650 uint64_t emlGetKey(int sectorNum, int keyType) {
651 uint8_t key[6];
652 uint8_t* emCARD = BigBuf_get_EM_addr();
653
654 memcpy(key, emCARD + 16 * (FirstBlockOfSector(sectorNum) + NumBlocksPerSector(sectorNum) - 1) + keyType * 10, 6);
655 return bytes_to_num(key, 6);
656 }
657
658 void emlClearMem(void) {
659 int b;
660
661 const uint8_t trailer[] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x07, 0x80, 0x69, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
662 const uint8_t uid[] = {0xe6, 0x84, 0x87, 0xf3, 0x16, 0x88, 0x04, 0x00, 0x46, 0x8e, 0x45, 0x55, 0x4d, 0x70, 0x41, 0x04};
663 uint8_t* emCARD = BigBuf_get_EM_addr();
664
665 memset(emCARD, 0, CARD_MEMORY_SIZE);
666
667 // fill sectors trailer data
668 for(b = 3; b < 256; b<127?(b+=4):(b+=16)) {
669 emlSetMem((uint8_t *)trailer, b , 1);
670 }
671
672 // uid
673 emlSetMem((uint8_t *)uid, 0, 1);
674 return;
675 }
676
677
678 // Mifare desfire commands
679 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)
680 {
681 uint8_t dcmd[5] = {0x00};
682 dcmd[0] = cmd;
683 memcpy(dcmd+1,data,2);
684 AppendCrc14443a(dcmd, 3);
685
686 ReaderTransmit(dcmd, sizeof(dcmd), NULL);
687 int len = ReaderReceive(answer, answer_parity);
688 if(!len) {
689 if (MF_DBGLEVEL >= MF_DBG_ERROR)
690 Dbprintf("Authentication failed. Card timeout.");
691 return 1;
692 }
693 return len;
694 }
695
696 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)
697 {
698 uint8_t dcmd[20] = {0x00};
699 dcmd[0] = cmd;
700 memcpy(dcmd+1,data,17);
701 AppendCrc14443a(dcmd, 18);
702
703 ReaderTransmit(dcmd, sizeof(dcmd), NULL);
704 int len = ReaderReceive(answer, answer_parity);
705 if(!len){
706 if (MF_DBGLEVEL >= MF_DBG_ERROR)
707 Dbprintf("Authentication failed. Card timeout.");
708 return 1;
709 }
710 return len;
711 }
712
713 int mifare_desfire_des_auth1(uint32_t uid, uint8_t *blockData){
714
715 int len;
716 // load key, keynumber
717 uint8_t data[2]={0x0a, 0x00};
718 uint8_t receivedAnswer[MAX_FRAME_SIZE];
719 uint8_t receivedAnswerPar[MAX_PARITY_SIZE];
720
721 len = mifare_sendcmd_special(NULL, 1, 0x02, data, receivedAnswer,receivedAnswerPar,NULL);
722 if (len == 1) {
723 if (MF_DBGLEVEL >= MF_DBG_ERROR)
724 Dbprintf("Cmd Error: %02x", receivedAnswer[0]);
725 return 1;
726 }
727
728 if (len == 12) {
729 if (MF_DBGLEVEL >= MF_DBG_EXTENDED) {
730 Dbprintf("Auth1 Resp: %02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",
731 receivedAnswer[0],receivedAnswer[1],receivedAnswer[2],receivedAnswer[3],receivedAnswer[4],
732 receivedAnswer[5],receivedAnswer[6],receivedAnswer[7],receivedAnswer[8],receivedAnswer[9],
733 receivedAnswer[10],receivedAnswer[11]);
734 }
735 memcpy(blockData, receivedAnswer, 12);
736 return 0;
737 }
738 return 1;
739 }
740
741 int mifare_desfire_des_auth2(uint32_t uid, uint8_t *key, uint8_t *blockData){
742
743 int len;
744 uint8_t data[17] = {0x00};
745 data[0] = 0xAF;
746 memcpy(data+1,key,16);
747
748 uint8_t receivedAnswer[MAX_MIFARE_FRAME_SIZE];
749 uint8_t receivedAnswerPar[MAX_MIFARE_PARITY_SIZE];
750
751 len = mifare_sendcmd_special2(NULL, 1, 0x03, data, receivedAnswer, receivedAnswerPar ,NULL);
752
753 if ((receivedAnswer[0] == 0x03) && (receivedAnswer[1] == 0xae)) {
754 if (MF_DBGLEVEL >= MF_DBG_ERROR)
755 Dbprintf("Auth Error: %02x %02x", receivedAnswer[0], receivedAnswer[1]);
756 return 1;
757 }
758
759 if (len == 12){
760 if (MF_DBGLEVEL >= MF_DBG_EXTENDED) {
761 Dbprintf("Auth2 Resp: %02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",
762 receivedAnswer[0],receivedAnswer[1],receivedAnswer[2],receivedAnswer[3],receivedAnswer[4],
763 receivedAnswer[5],receivedAnswer[6],receivedAnswer[7],receivedAnswer[8],receivedAnswer[9],
764 receivedAnswer[10],receivedAnswer[11]);
765 }
766 memcpy(blockData, receivedAnswer, 12);
767 return 0;
768 }
769 return 1;
770 }
771
772 //-----------------------------------------------------------------------------
773 // MIFARE check keys
774 //
775 //-----------------------------------------------------------------------------
776 // one key check
777 int MifareChkBlockKey(uint8_t *uid, uint32_t *cuid, uint8_t *cascade_levels, uint64_t ui64Key, uint8_t blockNo, uint8_t keyType, uint8_t debugLevel) {
778
779 struct Crypto1State mpcs = {0, 0};
780 struct Crypto1State *pcs;
781 pcs = &mpcs;
782
783 // Iceman: use piwi's faster nonce collecting part in hardnested.
784 if (*cascade_levels == 0) { // need a full select cycle to get the uid first
785 iso14a_card_select_t card_info;
786 if(!iso14443a_select_card(uid, &card_info, cuid, true, 0, true)) {
787 if (debugLevel >= 1) Dbprintf("ChkKeys: Can't select card");
788 return 1;
789 }
790 switch (card_info.uidlen) {
791 case 4 : *cascade_levels = 1; break;
792 case 7 : *cascade_levels = 2; break;
793 case 10: *cascade_levels = 3; break;
794 default: break;
795 }
796 } else { // no need for anticollision. We can directly select the card
797 if(!iso14443a_select_card(uid, NULL, NULL, false, *cascade_levels, true)) {
798 if (debugLevel >= 1) Dbprintf("ChkKeys: Can't select card (UID) lvl=%d", *cascade_levels);
799 return 1;
800 }
801 }
802
803 if(mifare_classic_auth(pcs, *cuid, blockNo, keyType, ui64Key, AUTH_FIRST)) {
804 // SpinDelayUs(AUTHENTICATION_TIMEOUT); // it not needs because mifare_classic_auth have timeout from iso14a_set_timeout()
805 return 2;
806 } else {
807 /* // let it be here. it like halt command, but maybe it will work in some strange cases
808 uint8_t dummy_answer = 0;
809 ReaderTransmit(&dummy_answer, 1, NULL);
810 int timeout = GetCountSspClk() + AUTHENTICATION_TIMEOUT;
811 // wait for the card to become ready again
812 while(GetCountSspClk() < timeout) {};
813 */
814 // it needs after success authentication
815 mifare_classic_halt(pcs, *cuid);
816 }
817
818 return 0;
819 }
820
821 // multi key check
822 int MifareChkBlockKeys(uint8_t *keys, uint8_t keyCount, uint8_t blockNo, uint8_t keyType, uint8_t debugLevel) {
823 uint8_t uid[10];
824 uint32_t cuid = 0;
825 uint8_t cascade_levels = 0;
826 uint64_t ui64Key = 0;
827
828 int retryCount = 0;
829 for (uint8_t i = 0; i < keyCount; i++) {
830
831 // Allow button press / usb cmd to interrupt device
832 if (BUTTON_PRESS() && !usb_poll_validate_length()) {
833 Dbprintf("ChkKeys: Cancel operation. Exit...");
834 return -2;
835 }
836
837 ui64Key = bytes_to_num(keys + i * 6, 6);
838 int res = MifareChkBlockKey(uid, &cuid, &cascade_levels, ui64Key, blockNo, keyType, debugLevel);
839
840 // can't select
841 if (res == 1) {
842 retryCount++;
843 if (retryCount >= 5) {
844 Dbprintf("ChkKeys: block=%d key=%d. Can't select. Exit...", blockNo, keyType);
845 return -1;
846 }
847 --i; // try the same key once again
848
849 SpinDelay(20);
850 // Dbprintf("ChkKeys: block=%d key=%d. Try the same key once again...", blockNo, keyType);
851 continue;
852 }
853
854 // can't authenticate
855 if (res == 2) {
856 retryCount = 0;
857 continue; // can't auth. wrong key.
858 }
859
860 return i + 1;
861 }
862
863 return 0;
864 }
865
866 // multisector multikey check
867 int MifareMultisectorChk(uint8_t *keys, uint8_t keyCount, uint8_t SectorCount, uint8_t keyType, uint8_t debugLevel, TKeyIndex *keyIndex) {
868 int res = 0;
869
870 // int clk = GetCountSspClk();
871
872 for(int sc = 0; sc < SectorCount; sc++){
873 WDT_HIT();
874
875 int keyAB = keyType;
876 do {
877 res = MifareChkBlockKeys(keys, keyCount, FirstBlockOfSector(sc), keyAB & 0x01, debugLevel);
878 if (res < 0){
879 return res;
880 }
881 if (res > 0){
882 (*keyIndex)[keyAB & 0x01][sc] = res;
883 }
884 } while(--keyAB > 0);
885 }
886
887 // Dbprintf("%d %d", GetCountSspClk() - clk, (GetCountSspClk() - clk)/(SectorCount*keyCount*(keyType==2?2:1)));
888
889 return 0;
890 }
891
892
Impressum, Datenschutz