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