]> git.zerfleddert.de Git - proxmark3-svn/blame_incremental - client/mifarehost.c
Update cmdhf14a.c
[proxmark3-svn] / client / mifarehost.c
... / ...
CommitLineData
1// Merlok, 2011, 2012\r
2// people from mifare@nethemba.com, 2010\r
3//\r
4// This code is licensed to you under the terms of the GNU GPL, version 2 or,\r
5// at your option, any later version. See the LICENSE.txt file for the text of\r
6// the license.\r
7//-----------------------------------------------------------------------------\r
8// mifare commands\r
9//-----------------------------------------------------------------------------\r
10\r
11#include "mifarehost.h"\r
12\r
13#include <stdio.h>\r
14#include <stdlib.h> \r
15#include <string.h>\r
16#include <pthread.h>\r
17\r
18#include "crapto1/crapto1.h"\r
19#include "proxmark3.h"\r
20#include "usb_cmd.h"\r
21#include "cmdmain.h"\r
22#include "ui.h"\r
23#include "util.h"\r
24#include "iso14443crc.h"\r
25\r
26// mifare tracer flags used in mfTraceDecode()\r
27#define TRACE_IDLE 0x00\r
28#define TRACE_AUTH1 0x01\r
29#define TRACE_AUTH2 0x02\r
30#define TRACE_AUTH_OK 0x03\r
31#define TRACE_READ_DATA 0x04\r
32#define TRACE_WRITE_OK 0x05\r
33#define TRACE_WRITE_DATA 0x06\r
34#define TRACE_ERROR 0xFF\r
35\r
36\r
37static int compare_uint64(const void *a, const void *b) {\r
38 // didn't work: (the result is truncated to 32 bits)\r
39 //return (*(int64_t*)b - *(int64_t*)a);\r
40\r
41 // better:\r
42 if (*(uint64_t*)b == *(uint64_t*)a) return 0;\r
43 else if (*(uint64_t*)b < *(uint64_t*)a) return 1;\r
44 else return -1;\r
45}\r
46\r
47\r
48// create the intersection (common members) of two sorted lists. Lists are terminated by -1. Result will be in list1. Number of elements is returned.\r
49static uint32_t intersection(uint64_t *list1, uint64_t *list2)\r
50{\r
51 if (list1 == NULL || list2 == NULL) {\r
52 return 0;\r
53 }\r
54 uint64_t *p1, *p2, *p3;\r
55 p1 = p3 = list1; \r
56 p2 = list2;\r
57\r
58 while ( *p1 != -1 && *p2 != -1 ) {\r
59 if (compare_uint64(p1, p2) == 0) {\r
60 *p3++ = *p1++;\r
61 p2++;\r
62 }\r
63 else {\r
64 while (compare_uint64(p1, p2) < 0) ++p1;\r
65 while (compare_uint64(p1, p2) > 0) ++p2;\r
66 }\r
67 }\r
68 *p3 = -1;\r
69 return p3 - list1;\r
70}\r
71\r
72\r
73// Darkside attack (hf mf mifare)\r
74static uint32_t nonce2key(uint32_t uid, uint32_t nt, uint32_t nr, uint64_t par_info, uint64_t ks_info, uint64_t **keys) {\r
75 struct Crypto1State *states;\r
76 uint32_t i, pos, rr; //nr_diff;\r
77 uint8_t bt, ks3x[8], par[8][8];\r
78 uint64_t key_recovered;\r
79 static uint64_t *keylist;\r
80 rr = 0;\r
81\r
82 // Reset the last three significant bits of the reader nonce\r
83 nr &= 0xffffff1f;\r
84\r
85 for (pos=0; pos<8; pos++) {\r
86 ks3x[7-pos] = (ks_info >> (pos*8)) & 0x0f;\r
87 bt = (par_info >> (pos*8)) & 0xff;\r
88 for (i=0; i<8; i++) {\r
89 par[7-pos][i] = (bt >> i) & 0x01;\r
90 }\r
91 }\r
92\r
93 states = lfsr_common_prefix(nr, rr, ks3x, par, (par_info == 0));\r
94\r
95 if (states == NULL) {\r
96 *keys = NULL;\r
97 return 0;\r
98 }\r
99\r
100 keylist = (uint64_t*)states;\r
101 \r
102 for (i = 0; keylist[i]; i++) {\r
103 lfsr_rollback_word(states+i, uid^nt, 0);\r
104 crypto1_get_lfsr(states+i, &key_recovered);\r
105 keylist[i] = key_recovered;\r
106 }\r
107 keylist[i] = -1;\r
108\r
109 *keys = keylist;\r
110 return i;\r
111}\r
112\r
113\r
114int mfDarkside(uint64_t *key)\r
115{\r
116 uint32_t uid = 0;\r
117 uint32_t nt = 0, nr = 0;\r
118 uint64_t par_list = 0, ks_list = 0;\r
119 uint64_t *keylist = NULL, *last_keylist = NULL;\r
120 uint32_t keycount = 0;\r
121 int16_t isOK = 0;\r
122\r
123 UsbCommand c = {CMD_READER_MIFARE, {true, 0, 0}};\r
124\r
125 // message\r
126 printf("-------------------------------------------------------------------------\n");\r
127 printf("Executing command. Expected execution time: 25sec on average\n");\r
128 printf("Press button on the proxmark3 device to abort both proxmark3 and client.\n");\r
129 printf("-------------------------------------------------------------------------\n");\r
130\r
131 \r
132 while (true) {\r
133 clearCommandBuffer();\r
134 SendCommand(&c);\r
135 \r
136 //flush queue\r
137 while (ukbhit()) {\r
138 int c = getchar(); (void) c;\r
139 }\r
140 \r
141 // wait cycle\r
142 while (true) {\r
143 printf(".");\r
144 fflush(stdout);\r
145 if (ukbhit()) {\r
146 return -5;\r
147 break;\r
148 }\r
149 \r
150 UsbCommand resp;\r
151 if (WaitForResponseTimeout(CMD_ACK, &resp, 1000)) {\r
152 isOK = resp.arg[0];\r
153 if (isOK < 0) {\r
154 return isOK;\r
155 }\r
156 uid = (uint32_t)bytes_to_num(resp.d.asBytes + 0, 4);\r
157 nt = (uint32_t)bytes_to_num(resp.d.asBytes + 4, 4);\r
158 par_list = bytes_to_num(resp.d.asBytes + 8, 8);\r
159 ks_list = bytes_to_num(resp.d.asBytes + 16, 8);\r
160 nr = bytes_to_num(resp.d.asBytes + 24, 4);\r
161 break;\r
162 }\r
163 } \r
164\r
165 if (par_list == 0 && c.arg[0] == true) {\r
166 PrintAndLog("Parity is all zero. Most likely this card sends NACK on every failed authentication.");\r
167 PrintAndLog("Attack will take a few seconds longer because we need two consecutive successful runs.");\r
168 }\r
169 c.arg[0] = false;\r
170\r
171 keycount = nonce2key(uid, nt, nr, par_list, ks_list, &keylist);\r
172\r
173 if (keycount == 0) {\r
174 PrintAndLog("Key not found (lfsr_common_prefix list is null). Nt=%08x", nt); \r
175 PrintAndLog("This is expected to happen in 25%% of all cases. Trying again with a different reader nonce...");\r
176 continue;\r
177 }\r
178\r
179 qsort(keylist, keycount, sizeof(*keylist), compare_uint64);\r
180 keycount = intersection(last_keylist, keylist);\r
181 if (keycount == 0) {\r
182 free(last_keylist);\r
183 last_keylist = keylist;\r
184 continue;\r
185 }\r
186\r
187 if (keycount > 1) {\r
188 PrintAndLog("Found %u possible keys. Trying to authenticate with each of them ...\n", keycount);\r
189 } else {\r
190 PrintAndLog("Found a possible key. Trying to authenticate...\n");\r
191 } \r
192\r
193 *key = -1;\r
194 uint8_t keyBlock[USB_CMD_DATA_SIZE];\r
195 int max_keys = USB_CMD_DATA_SIZE/6;\r
196 for (int i = 0; i < keycount; i += max_keys) {\r
197 int size = keycount - i > max_keys ? max_keys : keycount - i;\r
198 for (int j = 0; j < size; j++) {\r
199 if (last_keylist == NULL) {\r
200 num_to_bytes(keylist[i*max_keys + j], 6, keyBlock);\r
201 } else {\r
202 num_to_bytes(last_keylist[i*max_keys + j], 6, keyBlock);\r
203 }\r
204 }\r
205 if (!mfCheckKeys(0, 0, false, size, keyBlock, key)) {\r
206 break;\r
207 }\r
208 } \r
209 \r
210 if (*key != -1) {\r
211 free(last_keylist);\r
212 free(keylist);\r
213 break;\r
214 } else {\r
215 PrintAndLog("Authentication failed. Trying again...");\r
216 free(last_keylist);\r
217 last_keylist = keylist;\r
218 }\r
219 }\r
220 \r
221 return 0;\r
222}\r
223\r
224\r
225int mfCheckKeys (uint8_t blockNo, uint8_t keyType, bool clear_trace, uint8_t keycnt, uint8_t * keyBlock, uint64_t * key){\r
226\r
227 *key = -1;\r
228\r
229 UsbCommand c = {CMD_MIFARE_CHKKEYS, {((blockNo & 0xff) | ((keyType&0xff)<<8)), clear_trace, keycnt}};\r
230 memcpy(c.d.asBytes, keyBlock, 6 * keycnt);\r
231 SendCommand(&c);\r
232\r
233 UsbCommand resp;\r
234 if (!WaitForResponseTimeout(CMD_ACK,&resp,3000)) return 1;\r
235 if ((resp.arg[0] & 0xff) != 0x01) return 2;\r
236 *key = bytes_to_num(resp.d.asBytes, 6);\r
237 return 0;\r
238}\r
239\r
240// Compare 16 Bits out of cryptostate\r
241int Compare16Bits(const void * a, const void * b) {\r
242 if ((*(uint64_t*)b & 0x00ff000000ff0000) == (*(uint64_t*)a & 0x00ff000000ff0000)) return 0;\r
243 else if ((*(uint64_t*)b & 0x00ff000000ff0000) > (*(uint64_t*)a & 0x00ff000000ff0000)) return 1;\r
244 else return -1;\r
245}\r
246\r
247typedef \r
248 struct {\r
249 union {\r
250 struct Crypto1State *slhead;\r
251 uint64_t *keyhead;\r
252 } head;\r
253 union {\r
254 struct Crypto1State *sltail;\r
255 uint64_t *keytail;\r
256 } tail;\r
257 uint32_t len;\r
258 uint32_t uid;\r
259 uint32_t blockNo;\r
260 uint32_t keyType;\r
261 uint32_t nt;\r
262 uint32_t ks1;\r
263 } StateList_t;\r
264\r
265\r
266// wrapper function for multi-threaded lfsr_recovery32\r
267void* nested_worker_thread(void *arg)\r
268{\r
269 struct Crypto1State *p1;\r
270 StateList_t *statelist = arg;\r
271\r
272 statelist->head.slhead = lfsr_recovery32(statelist->ks1, statelist->nt ^ statelist->uid);\r
273 for (p1 = statelist->head.slhead; *(uint64_t *)p1 != 0; p1++);\r
274 statelist->len = p1 - statelist->head.slhead;\r
275 statelist->tail.sltail = --p1;\r
276 qsort(statelist->head.slhead, statelist->len, sizeof(uint64_t), Compare16Bits);\r
277 \r
278 return statelist->head.slhead;\r
279}\r
280\r
281int mfnested(uint8_t blockNo, uint8_t keyType, uint8_t *key, uint8_t trgBlockNo, uint8_t trgKeyType, uint8_t *resultKey, bool calibrate) \r
282{\r
283 uint16_t i;\r
284 uint32_t uid;\r
285 UsbCommand resp;\r
286\r
287 StateList_t statelists[2];\r
288 struct Crypto1State *p1, *p2, *p3, *p4;\r
289 \r
290 // flush queue\r
291 WaitForResponseTimeout(CMD_ACK, NULL, 100);\r
292 \r
293 UsbCommand c = {CMD_MIFARE_NESTED, {blockNo + keyType * 0x100, trgBlockNo + trgKeyType * 0x100, calibrate}};\r
294 memcpy(c.d.asBytes, key, 6);\r
295 SendCommand(&c);\r
296\r
297 if (!WaitForResponseTimeout(CMD_ACK, &resp, 1500)) {\r
298 return -1;\r
299 }\r
300\r
301 if (resp.arg[0]) {\r
302 return resp.arg[0]; // error during nested\r
303 }\r
304 \r
305 memcpy(&uid, resp.d.asBytes, 4);\r
306 PrintAndLog("uid:%08x trgbl=%d trgkey=%x", uid, (uint16_t)resp.arg[2] & 0xff, (uint16_t)resp.arg[2] >> 8);\r
307 \r
308 for (i = 0; i < 2; i++) {\r
309 statelists[i].blockNo = resp.arg[2] & 0xff;\r
310 statelists[i].keyType = (resp.arg[2] >> 8) & 0xff;\r
311 statelists[i].uid = uid;\r
312 memcpy(&statelists[i].nt, (void *)(resp.d.asBytes + 4 + i * 8 + 0), 4);\r
313 memcpy(&statelists[i].ks1, (void *)(resp.d.asBytes + 4 + i * 8 + 4), 4);\r
314 }\r
315 \r
316 // calc keys\r
317 \r
318 pthread_t thread_id[2];\r
319 \r
320 // create and run worker threads\r
321 for (i = 0; i < 2; i++) {\r
322 pthread_create(thread_id + i, NULL, nested_worker_thread, &statelists[i]);\r
323 }\r
324 \r
325 // wait for threads to terminate:\r
326 for (i = 0; i < 2; i++) {\r
327 pthread_join(thread_id[i], (void*)&statelists[i].head.slhead);\r
328 }\r
329\r
330\r
331 // the first 16 Bits of the cryptostate already contain part of our key.\r
332 // Create the intersection of the two lists based on these 16 Bits and\r
333 // roll back the cryptostate\r
334 p1 = p3 = statelists[0].head.slhead; \r
335 p2 = p4 = statelists[1].head.slhead;\r
336 while (p1 <= statelists[0].tail.sltail && p2 <= statelists[1].tail.sltail) {\r
337 if (Compare16Bits(p1, p2) == 0) {\r
338 struct Crypto1State savestate, *savep = &savestate;\r
339 savestate = *p1;\r
340 while(Compare16Bits(p1, savep) == 0 && p1 <= statelists[0].tail.sltail) {\r
341 *p3 = *p1;\r
342 lfsr_rollback_word(p3, statelists[0].nt ^ statelists[0].uid, 0);\r
343 p3++;\r
344 p1++;\r
345 }\r
346 savestate = *p2;\r
347 while(Compare16Bits(p2, savep) == 0 && p2 <= statelists[1].tail.sltail) {\r
348 *p4 = *p2;\r
349 lfsr_rollback_word(p4, statelists[1].nt ^ statelists[1].uid, 0);\r
350 p4++;\r
351 p2++;\r
352 }\r
353 }\r
354 else {\r
355 while (Compare16Bits(p1, p2) == -1) p1++;\r
356 while (Compare16Bits(p1, p2) == 1) p2++;\r
357 }\r
358 }\r
359 *(uint64_t*)p3 = -1;\r
360 *(uint64_t*)p4 = -1;\r
361 statelists[0].len = p3 - statelists[0].head.slhead;\r
362 statelists[1].len = p4 - statelists[1].head.slhead;\r
363 statelists[0].tail.sltail=--p3;\r
364 statelists[1].tail.sltail=--p4;\r
365\r
366 // the statelists now contain possible keys. The key we are searching for must be in the\r
367 // intersection of both lists. Create the intersection:\r
368 qsort(statelists[0].head.keyhead, statelists[0].len, sizeof(uint64_t), compare_uint64);\r
369 qsort(statelists[1].head.keyhead, statelists[1].len, sizeof(uint64_t), compare_uint64);\r
370 statelists[0].len = intersection(statelists[0].head.keyhead, statelists[1].head.keyhead);\r
371\r
372 memset(resultKey, 0, 6);\r
373 // The list may still contain several key candidates. Test each of them with mfCheckKeys\r
374 for (i = 0; i < statelists[0].len; i++) {\r
375 uint8_t keyBlock[6];\r
376 uint64_t key64;\r
377 crypto1_get_lfsr(statelists[0].head.slhead + i, &key64);\r
378 num_to_bytes(key64, 6, keyBlock);\r
379 key64 = 0;\r
380 if (!mfCheckKeys(statelists[0].blockNo, statelists[0].keyType, false, 1, keyBlock, &key64)) {\r
381 num_to_bytes(key64, 6, resultKey);\r
382 break;\r
383 }\r
384 }\r
385 \r
386 free(statelists[0].head.slhead);\r
387 free(statelists[1].head.slhead);\r
388 \r
389 return 0;\r
390}\r
391\r
392// EMULATOR\r
393\r
394int mfEmlGetMem(uint8_t *data, int blockNum, int blocksCount) {\r
395 UsbCommand c = {CMD_MIFARE_EML_MEMGET, {blockNum, blocksCount, 0}};\r
396 SendCommand(&c);\r
397\r
398 UsbCommand resp;\r
399 if (!WaitForResponseTimeout(CMD_ACK,&resp,1500)) return 1;\r
400 memcpy(data, resp.d.asBytes, blocksCount * 16);\r
401 return 0;\r
402}\r
403\r
404int mfEmlSetMem(uint8_t *data, int blockNum, int blocksCount) {\r
405 UsbCommand c = {CMD_MIFARE_EML_MEMSET, {blockNum, blocksCount, 0}};\r
406 memcpy(c.d.asBytes, data, blocksCount * 16); \r
407 SendCommand(&c);\r
408 return 0;\r
409}\r
410\r
411// "MAGIC" CARD\r
412\r
413int mfCGetBlock(uint8_t blockNo, uint8_t *data, uint8_t params) {\r
414 uint8_t isOK = 0;\r
415\r
416 UsbCommand c = {CMD_MIFARE_CGETBLOCK, {params, 0, blockNo}};\r
417 SendCommand(&c);\r
418\r
419 UsbCommand resp;\r
420 if (WaitForResponseTimeout(CMD_ACK,&resp,1500)) {\r
421 isOK = resp.arg[0] & 0xff;\r
422 memcpy(data, resp.d.asBytes, 16);\r
423 if (!isOK) return 2;\r
424 } else {\r
425 PrintAndLog("Command execute timeout");\r
426 return 1;\r
427 }\r
428 return 0;\r
429}\r
430\r
431int mfCSetBlock(uint8_t blockNo, uint8_t *data, uint8_t *uid, bool wantWipe, uint8_t params) {\r
432\r
433 uint8_t isOK = 0;\r
434 UsbCommand c = {CMD_MIFARE_CSETBLOCK, {wantWipe, params & (0xFE | (uid == NULL ? 0:1)), blockNo}};\r
435 memcpy(c.d.asBytes, data, 16); \r
436 SendCommand(&c);\r
437\r
438 UsbCommand resp;\r
439 if (WaitForResponseTimeout(CMD_ACK,&resp,1500)) {\r
440 isOK = resp.arg[0] & 0xff;\r
441 if (uid != NULL) \r
442 memcpy(uid, resp.d.asBytes, 4);\r
443 if (!isOK) \r
444 return 2;\r
445 } else {\r
446 PrintAndLog("Command execute timeout");\r
447 return 1;\r
448 }\r
449 return 0;\r
450}\r
451\r
452int mfCSetUID(uint8_t *uid, uint8_t *atqa, uint8_t *sak, uint8_t *oldUID, bool wantWipe) {\r
453 uint8_t oldblock0[16] = {0x00};\r
454 uint8_t block0[16] = {0x00};\r
455\r
456 int old = mfCGetBlock(0, oldblock0, CSETBLOCK_SINGLE_OPER);\r
457 if (old == 0) {\r
458 memcpy(block0, oldblock0, 16);\r
459 PrintAndLog("old block 0: %s", sprint_hex(block0,16));\r
460 } else {\r
461 PrintAndLog("Couldn't get old data. Will write over the last bytes of Block 0.");\r
462 }\r
463\r
464 // fill in the new values\r
465 // UID\r
466 memcpy(block0, uid, 4); \r
467 // Mifare UID BCC\r
468 block0[4] = block0[0]^block0[1]^block0[2]^block0[3];\r
469 // mifare classic SAK(byte 5) and ATQA(byte 6 and 7, reversed)\r
470 if (sak!=NULL)\r
471 block0[5]=sak[0];\r
472 if (atqa!=NULL) {\r
473 block0[6]=atqa[1];\r
474 block0[7]=atqa[0];\r
475 }\r
476 PrintAndLog("new block 0: %s", sprint_hex(block0,16));\r
477 return mfCSetBlock(0, block0, oldUID, wantWipe, CSETBLOCK_SINGLE_OPER);\r
478}\r
479\r
480// SNIFFER\r
481\r
482// constants\r
483static uint8_t trailerAccessBytes[4] = {0x08, 0x77, 0x8F, 0x00};\r
484\r
485// variables\r
486char logHexFileName[FILE_PATH_SIZE] = {0x00};\r
487static uint8_t traceCard[4096] = {0x00};\r
488static char traceFileName[FILE_PATH_SIZE] = {0x00};\r
489static int traceState = TRACE_IDLE;\r
490static uint8_t traceCurBlock = 0;\r
491static uint8_t traceCurKey = 0;\r
492\r
493struct Crypto1State *traceCrypto1 = NULL;\r
494\r
495struct Crypto1State *revstate;\r
496uint64_t lfsr;\r
497uint32_t ks2;\r
498uint32_t ks3;\r
499\r
500uint32_t uid; // serial number\r
501uint32_t nt; // tag challenge\r
502uint32_t nr_enc; // encrypted reader challenge\r
503uint32_t ar_enc; // encrypted reader response\r
504uint32_t at_enc; // encrypted tag response\r
505\r
506int isTraceCardEmpty(void) {\r
507 return ((traceCard[0] == 0) && (traceCard[1] == 0) && (traceCard[2] == 0) && (traceCard[3] == 0));\r
508}\r
509\r
510int isBlockEmpty(int blockN) {\r
511 for (int i = 0; i < 16; i++) \r
512 if (traceCard[blockN * 16 + i] != 0) return 0;\r
513\r
514 return 1;\r
515}\r
516\r
517int isBlockTrailer(int blockN) {\r
518 return ((blockN & 0x03) == 0x03);\r
519}\r
520\r
521int saveTraceCard(void) {\r
522 FILE * f;\r
523 \r
524 if ((!strlen(traceFileName)) || (isTraceCardEmpty())) return 0;\r
525 \r
526 f = fopen(traceFileName, "w+");\r
527 if ( !f ) return 1;\r
528 \r
529 for (int i = 0; i < 64; i++) { // blocks\r
530 for (int j = 0; j < 16; j++) // bytes\r
531 fprintf(f, "%02x", *(traceCard + i * 16 + j)); \r
532 fprintf(f,"\n");\r
533 }\r
534 fclose(f);\r
535 return 0;\r
536}\r
537\r
538int loadTraceCard(uint8_t *tuid) {\r
539 FILE * f;\r
540 char buf[64] = {0x00};\r
541 uint8_t buf8[64] = {0x00};\r
542 int i, blockNum;\r
543 \r
544 if (!isTraceCardEmpty()) \r
545 saveTraceCard();\r
546 \r
547 memset(traceCard, 0x00, 4096);\r
548 memcpy(traceCard, tuid + 3, 4);\r
549\r
550 FillFileNameByUID(traceFileName, tuid, ".eml", 7);\r
551\r
552 f = fopen(traceFileName, "r");\r
553 if (!f) return 1;\r
554 \r
555 blockNum = 0;\r
556 \r
557 while(!feof(f)){\r
558 \r
559 memset(buf, 0, sizeof(buf));\r
560 if (fgets(buf, sizeof(buf), f) == NULL) {\r
561 PrintAndLog("File reading error.");\r
562 fclose(f);\r
563 return 2;\r
564 }\r
565\r
566 if (strlen(buf) < 32){\r
567 if (feof(f)) break;\r
568 PrintAndLog("File content error. Block data must include 32 HEX symbols");\r
569 fclose(f);\r
570 return 2;\r
571 }\r
572 for (i = 0; i < 32; i += 2)\r
573 sscanf(&buf[i], "%02x", (unsigned int *)&buf8[i / 2]);\r
574\r
575 memcpy(traceCard + blockNum * 16, buf8, 16);\r
576\r
577 blockNum++;\r
578 }\r
579 fclose(f);\r
580\r
581 return 0;\r
582}\r
583\r
584int mfTraceInit(uint8_t *tuid, uint8_t *atqa, uint8_t sak, bool wantSaveToEmlFile) {\r
585\r
586 if (traceCrypto1) \r
587 crypto1_destroy(traceCrypto1);\r
588\r
589 traceCrypto1 = NULL;\r
590\r
591 if (wantSaveToEmlFile) \r
592 loadTraceCard(tuid);\r
593 \r
594 traceCard[4] = traceCard[0] ^ traceCard[1] ^ traceCard[2] ^ traceCard[3];\r
595 traceCard[5] = sak;\r
596 memcpy(&traceCard[6], atqa, 2);\r
597 traceCurBlock = 0;\r
598 uid = bytes_to_num(tuid + 3, 4);\r
599 \r
600 traceState = TRACE_IDLE;\r
601\r
602 return 0;\r
603}\r
604\r
605void mf_crypto1_decrypt(struct Crypto1State *pcs, uint8_t *data, int len, bool isEncrypted){\r
606 uint8_t bt = 0;\r
607 int i;\r
608 \r
609 if (len != 1) {\r
610 for (i = 0; i < len; i++)\r
611 data[i] = crypto1_byte(pcs, 0x00, isEncrypted) ^ data[i];\r
612 } else {\r
613 bt = 0;\r
614 for (i = 0; i < 4; i++)\r
615 bt |= (crypto1_bit(pcs, 0, isEncrypted) ^ BIT(data[0], i)) << i;\r
616 \r
617 data[0] = bt;\r
618 }\r
619 return;\r
620}\r
621\r
622\r
623int mfTraceDecode(uint8_t *data_src, int len, bool wantSaveToEmlFile) {\r
624 uint8_t data[64];\r
625\r
626 if (traceState == TRACE_ERROR) return 1;\r
627 if (len > 64) {\r
628 traceState = TRACE_ERROR;\r
629 return 1;\r
630 }\r
631 \r
632 memcpy(data, data_src, len);\r
633 if ((traceCrypto1) && ((traceState == TRACE_IDLE) || (traceState > TRACE_AUTH_OK))) {\r
634 mf_crypto1_decrypt(traceCrypto1, data, len, 0);\r
635 PrintAndLog("dec> %s", sprint_hex(data, len));\r
636 AddLogHex(logHexFileName, "dec> ", data, len); \r
637 }\r
638 \r
639 switch (traceState) {\r
640 case TRACE_IDLE: \r
641 // check packet crc16!\r
642 if ((len >= 4) && (!CheckCrc14443(CRC_14443_A, data, len))) {\r
643 PrintAndLog("dec> CRC ERROR!!!");\r
644 AddLogLine(logHexFileName, "dec> ", "CRC ERROR!!!"); \r
645 traceState = TRACE_ERROR; // do not decrypt the next commands\r
646 return 1;\r
647 }\r
648 \r
649 // AUTHENTICATION\r
650 if ((len ==4) && ((data[0] == 0x60) || (data[0] == 0x61))) {\r
651 traceState = TRACE_AUTH1;\r
652 traceCurBlock = data[1];\r
653 traceCurKey = data[0] == 60 ? 1:0;\r
654 return 0;\r
655 }\r
656\r
657 // READ\r
658 if ((len ==4) && ((data[0] == 0x30))) {\r
659 traceState = TRACE_READ_DATA;\r
660 traceCurBlock = data[1];\r
661 return 0;\r
662 }\r
663\r
664 // WRITE\r
665 if ((len ==4) && ((data[0] == 0xA0))) {\r
666 traceState = TRACE_WRITE_OK;\r
667 traceCurBlock = data[1];\r
668 return 0;\r
669 }\r
670\r
671 // HALT\r
672 if ((len ==4) && ((data[0] == 0x50) && (data[1] == 0x00))) {\r
673 traceState = TRACE_ERROR; // do not decrypt the next commands\r
674 return 0;\r
675 }\r
676 \r
677 return 0;\r
678 break;\r
679 \r
680 case TRACE_READ_DATA: \r
681 if (len == 18) {\r
682 traceState = TRACE_IDLE;\r
683\r
684 if (isBlockTrailer(traceCurBlock)) {\r
685 memcpy(traceCard + traceCurBlock * 16 + 6, data + 6, 4);\r
686 } else {\r
687 memcpy(traceCard + traceCurBlock * 16, data, 16);\r
688 }\r
689 if (wantSaveToEmlFile) saveTraceCard();\r
690 return 0;\r
691 } else {\r
692 traceState = TRACE_ERROR;\r
693 return 1;\r
694 }\r
695 break;\r
696\r
697 case TRACE_WRITE_OK: \r
698 if ((len == 1) && (data[0] == 0x0a)) {\r
699 traceState = TRACE_WRITE_DATA;\r
700\r
701 return 0;\r
702 } else {\r
703 traceState = TRACE_ERROR;\r
704 return 1;\r
705 }\r
706 break;\r
707\r
708 case TRACE_WRITE_DATA: \r
709 if (len == 18) {\r
710 traceState = TRACE_IDLE;\r
711\r
712 memcpy(traceCard + traceCurBlock * 16, data, 16);\r
713 if (wantSaveToEmlFile) saveTraceCard();\r
714 return 0;\r
715 } else {\r
716 traceState = TRACE_ERROR;\r
717 return 1;\r
718 }\r
719 break;\r
720\r
721 case TRACE_AUTH1: \r
722 if (len == 4) {\r
723 traceState = TRACE_AUTH2;\r
724 nt = bytes_to_num(data, 4);\r
725 return 0;\r
726 } else {\r
727 traceState = TRACE_ERROR;\r
728 return 1;\r
729 }\r
730 break;\r
731\r
732 case TRACE_AUTH2: \r
733 if (len == 8) {\r
734 traceState = TRACE_AUTH_OK;\r
735\r
736 nr_enc = bytes_to_num(data, 4);\r
737 ar_enc = bytes_to_num(data + 4, 4);\r
738 return 0;\r
739 } else {\r
740 traceState = TRACE_ERROR;\r
741 return 1;\r
742 }\r
743 break;\r
744\r
745 case TRACE_AUTH_OK: \r
746 if (len ==4) {\r
747 traceState = TRACE_IDLE;\r
748\r
749 at_enc = bytes_to_num(data, 4);\r
750 \r
751 // decode key here)\r
752 ks2 = ar_enc ^ prng_successor(nt, 64);\r
753 ks3 = at_enc ^ prng_successor(nt, 96);\r
754 revstate = lfsr_recovery64(ks2, ks3);\r
755 lfsr_rollback_word(revstate, 0, 0);\r
756 lfsr_rollback_word(revstate, 0, 0);\r
757 lfsr_rollback_word(revstate, nr_enc, 1);\r
758 lfsr_rollback_word(revstate, uid ^ nt, 0);\r
759\r
760 crypto1_get_lfsr(revstate, &lfsr);\r
761 printf("key> %x%x\n", (unsigned int)((lfsr & 0xFFFFFFFF00000000) >> 32), (unsigned int)(lfsr & 0xFFFFFFFF));\r
762 AddLogUint64(logHexFileName, "key> ", lfsr); \r
763 \r
764 int blockShift = ((traceCurBlock & 0xFC) + 3) * 16;\r
765 if (isBlockEmpty((traceCurBlock & 0xFC) + 3)) memcpy(traceCard + blockShift + 6, trailerAccessBytes, 4);\r
766 \r
767 if (traceCurKey) {\r
768 num_to_bytes(lfsr, 6, traceCard + blockShift + 10);\r
769 } else {\r
770 num_to_bytes(lfsr, 6, traceCard + blockShift);\r
771 }\r
772 if (wantSaveToEmlFile) saveTraceCard();\r
773\r
774 if (traceCrypto1) {\r
775 crypto1_destroy(traceCrypto1);\r
776 }\r
777 \r
778 // set cryptosystem state\r
779 traceCrypto1 = lfsr_recovery64(ks2, ks3);\r
780 \r
781// nt = crypto1_word(traceCrypto1, nt ^ uid, 1) ^ nt;\r
782\r
783 /* traceCrypto1 = crypto1_create(lfsr); // key in lfsr\r
784 crypto1_word(traceCrypto1, nt ^ uid, 0);\r
785 crypto1_word(traceCrypto1, ar, 1);\r
786 crypto1_word(traceCrypto1, 0, 0);\r
787 crypto1_word(traceCrypto1, 0, 0);*/\r
788 \r
789 return 0;\r
790 } else {\r
791 traceState = TRACE_ERROR;\r
792 return 1;\r
793 }\r
794 break;\r
795\r
796 default: \r
797 traceState = TRACE_ERROR;\r
798 return 1;\r
799 }\r
800\r
801 return 0;\r
802}\r
803\r
804int tryDecryptWord(uint32_t nt, uint32_t ar_enc, uint32_t at_enc, uint8_t *data, int len){\r
805 /*\r
806 uint32_t nt; // tag challenge\r
807 uint32_t ar_enc; // encrypted reader response\r
808 uint32_t at_enc; // encrypted tag response\r
809 */\r
810 if (traceCrypto1) {\r
811 crypto1_destroy(traceCrypto1);\r
812 }\r
813 ks2 = ar_enc ^ prng_successor(nt, 64);\r
814 ks3 = at_enc ^ prng_successor(nt, 96);\r
815 traceCrypto1 = lfsr_recovery64(ks2, ks3);\r
816\r
817 mf_crypto1_decrypt(traceCrypto1, data, len, 0);\r
818\r
819 PrintAndLog("Decrypted data: [%s]", sprint_hex(data,len) );\r
820 crypto1_destroy(traceCrypto1);\r
821 return 0;\r
822}\r
823/* Detect Tag Prng, \r
824* function performs a partial AUTH, where it tries to authenticate against block0, key A, but only collects tag nonce.\r
825* the tag nonce is check to see if it has a predictable PRNG.\r
826* @returns \r
827* TRUE if tag uses WEAK prng (ie Darkside attack possible)\r
828* FALSE is tag uses HARDEND prng (ie hardnested attack possible, with known key)\r
829*/\r
830bool detect_classic_prng(){\r
831\r
832 UsbCommand resp, respA; \r
833 uint8_t cmd[] = {MIFARE_AUTH_KEYA, 0x00};\r
834 uint32_t flags = ISO14A_CONNECT | ISO14A_RAW | ISO14A_APPEND_CRC;\r
835 \r
836 UsbCommand cAuth = {CMD_READER_ISO_14443a, {flags, sizeof(cmd), 0}};\r
837 memcpy(cAuth.d.asBytes, cmd, sizeof(cmd));\r
838\r
839 clearCommandBuffer();\r
840 SendCommand(&cAuth);\r
841 WaitForResponse(CMD_ACK, &resp);\r
842 WaitForResponse(CMD_ACK, &respA);\r
843 \r
844 // if select tag failed.\r
845 if ( resp.arg[0] == 0 ) {\r
846 printf("Error: selecting tag failed, can't detect prng\n");\r
847 return false;\r
848 }\r
849\r
850 uint32_t nonce = bytes_to_num(respA.d.asBytes, respA.arg[0]);\r
851 return validate_prng_nonce(nonce);\r
852}\r
Impressum, Datenschutz