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