]> git.zerfleddert.de Git - proxmark3-svn/blame - client/mifarehost.c
add: added a noncedistance method
[proxmark3-svn] / client / mifarehost.c
CommitLineData
b62a5a84 1// Merlok, 2011, 2012\r
f397b5cc
M
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
55acbb2a 8// mifare commands\r
f397b5cc
M
9//-----------------------------------------------------------------------------\r
10\r
11#include <stdio.h>\r
873014de 12#include <stdlib.h> \r
31b6e9af 13#include <string.h>\r
9492e0b0 14#include <pthread.h>\r
f397b5cc 15#include "mifarehost.h"\r
902cb3c0 16#include "proxmark3.h"\r
c9f8bc53 17//#include "radixsort.h"\r
f397b5cc 18\r
55acbb2a 19// MIFARE\r
f397b5cc 20int compar_int(const void * a, const void * b) {\r
9492e0b0 21 // didn't work: (the result is truncated to 32 bits)\r
22 //return (*(uint64_t*)b - *(uint64_t*)a);\r
23\r
24 // better:\r
6b10e329 25 if (*(uint64_t*)b < *(uint64_t*)a) return -1;\r
26 if (*(uint64_t*)b > *(uint64_t*)a) return 1;\r
27 return 0;\r
d294f582 28\r
6b10e329 29 //return (*(uint64_t*)b > *(uint64_t*)a) - (*(uint64_t*)b < *(uint64_t*)a);\r
f397b5cc
M
30}\r
31\r
9492e0b0 32// Compare 16 Bits out of cryptostate\r
33int Compare16Bits(const void * a, const void * b) {\r
d294f582 34\r
6b10e329 35 if ((*(uint64_t*)b & 0x00ff000000ff0000) < (*(uint64_t*)a & 0x00ff000000ff0000)) return -1;\r
36 if ((*(uint64_t*)b & 0x00ff000000ff0000) > (*(uint64_t*)a & 0x00ff000000ff0000)) return 1; \r
37 return 0;\r
d294f582 38\r
6b10e329 39 /*return \r
d294f582 40 ((*(uint64_t*)b & 0x00ff000000ff0000) > (*(uint64_t*)a & 0x00ff000000ff0000))\r
41 -\r
42 ((*(uint64_t*)b & 0x00ff000000ff0000) < (*(uint64_t*)a & 0x00ff000000ff0000))\r
43 ;\r
6b10e329 44*/\r
f397b5cc
M
45}\r
46\r
9492e0b0 47typedef \r
48 struct {\r
49 union {\r
50 struct Crypto1State *slhead;\r
51 uint64_t *keyhead;\r
3f7c993d 52 } head;\r
9492e0b0 53 union {\r
54 struct Crypto1State *sltail;\r
55 uint64_t *keytail;\r
3f7c993d 56 } tail;\r
9492e0b0 57 uint32_t len;\r
58 uint32_t uid;\r
59 uint32_t blockNo;\r
60 uint32_t keyType;\r
61 uint32_t nt;\r
62 uint32_t ks1;\r
63 } StateList_t;\r
64\r
65\r
66// wrapper function for multi-threaded lfsr_recovery32\r
67void* nested_worker_thread(void *arg)\r
68{\r
69 struct Crypto1State *p1;\r
70 StateList_t *statelist = arg;\r
71\r
3f7c993d 72 statelist->head.slhead = lfsr_recovery32(statelist->ks1, statelist->nt ^ statelist->uid);\r
d294f582 73 \r
6b10e329 74 for (p1 = statelist->head.slhead; *(uint64_t *)p1 != 0; p1++);\r
d294f582 75 \r
3f7c993d 76 statelist->len = p1 - statelist->head.slhead;\r
77 statelist->tail.sltail = --p1;\r
78 qsort(statelist->head.slhead, statelist->len, sizeof(uint64_t), Compare16Bits);\r
f397b5cc 79 \r
3f7c993d 80 return statelist->head.slhead;\r
f397b5cc
M
81}\r
82\r
9492e0b0 83int mfnested(uint8_t blockNo, uint8_t keyType, uint8_t * key, uint8_t trgBlockNo, uint8_t trgKeyType, uint8_t * resultKey, bool calibrate) \r
f397b5cc 84{\r
c830303d 85 uint16_t i;\r
f397b5cc 86 uint32_t uid;\r
902cb3c0 87 UsbCommand resp;\r
9492e0b0 88 StateList_t statelists[2];\r
89 struct Crypto1State *p1, *p2, *p3, *p4;\r
90 \r
9492e0b0 91 UsbCommand c = {CMD_MIFARE_NESTED, {blockNo + keyType * 0x100, trgBlockNo + trgKeyType * 0x100, calibrate}};\r
f397b5cc 92 memcpy(c.d.asBytes, key, 6);\r
952f3dc7 93 clearCommandBuffer();\r
9492e0b0 94 SendCommand(&c);\r
952f3dc7 95 if (!WaitForResponseTimeout(CMD_ACK, &resp, 1500)) return -1;\r
c830303d 96\r
952f3dc7 97 // error during nested\r
98 if (resp.arg[0]) return resp.arg[0];\r
99 \r
6b10e329 100// memcpy(&uid, resp.d.asBytes, 4);\r
101 uid = bytes_to_num(resp.d.asBytes, 4);\r
102 \r
103 for (i = 0; i < 2; i++) {\r
0de8e387 104 statelists[i].blockNo = resp.arg[2] & 0xff;\r
105 statelists[i].keyType = (resp.arg[2] >> 8) & 0xff;\r
106 statelists[i].uid = uid;\r
107 memcpy(&statelists[i].nt, (void *)(resp.d.asBytes + 4 + i * 8 + 0), 4);\r
108 memcpy(&statelists[i].ks1, (void *)(resp.d.asBytes + 4 + i * 8 + 4), 4);\r
109 }\r
f397b5cc 110 \r
d294f582 111 // calc keys \r
9492e0b0 112 pthread_t thread_id[2];\r
113 \r
114 // create and run worker threads\r
d294f582 115 for (i = 0; i < 2; i++)\r
9492e0b0 116 pthread_create(thread_id + i, NULL, nested_worker_thread, &statelists[i]);\r
6b10e329 117\r
9492e0b0 118 // wait for threads to terminate:\r
d294f582 119 for (i = 0; i < 2; i++)\r
3f7c993d 120 pthread_join(thread_id[i], (void*)&statelists[i].head.slhead);\r
f397b5cc 121\r
9492e0b0 122 // the first 16 Bits of the cryptostate already contain part of our key.\r
123 // Create the intersection of the two lists based on these 16 Bits and\r
124 // roll back the cryptostate\r
3f7c993d 125 p1 = p3 = statelists[0].head.slhead; \r
126 p2 = p4 = statelists[1].head.slhead;\r
6b10e329 127\r
3f7c993d 128 while (p1 <= statelists[0].tail.sltail && p2 <= statelists[1].tail.sltail) {\r
9492e0b0 129 if (Compare16Bits(p1, p2) == 0) {\r
6b10e329 130 \r
9492e0b0 131 struct Crypto1State savestate, *savep = &savestate;\r
132 savestate = *p1;\r
3f7c993d 133 while(Compare16Bits(p1, savep) == 0 && p1 <= statelists[0].tail.sltail) {\r
9492e0b0 134 *p3 = *p1;\r
135 lfsr_rollback_word(p3, statelists[0].nt ^ statelists[0].uid, 0);\r
136 p3++;\r
137 p1++;\r
138 }\r
139 savestate = *p2;\r
3f7c993d 140 while(Compare16Bits(p2, savep) == 0 && p2 <= statelists[1].tail.sltail) {\r
9492e0b0 141 *p4 = *p2;\r
142 lfsr_rollback_word(p4, statelists[1].nt ^ statelists[1].uid, 0);\r
143 p4++;\r
144 p2++;\r
145 }\r
146 }\r
147 else {\r
148 while (Compare16Bits(p1, p2) == -1) p1++;\r
149 while (Compare16Bits(p1, p2) == 1) p2++;\r
150 }\r
f397b5cc 151 }\r
d294f582 152 \r
9492e0b0 153 p3->even = 0; p3->odd = 0;\r
154 p4->even = 0; p4->odd = 0;\r
3f7c993d 155 statelists[0].len = p3 - statelists[0].head.slhead;\r
156 statelists[1].len = p4 - statelists[1].head.slhead;\r
157 statelists[0].tail.sltail=--p3;\r
158 statelists[1].tail.sltail=--p4;\r
9492e0b0 159\r
160 // the statelists now contain possible keys. The key we are searching for must be in the\r
161 // intersection of both lists. Create the intersection:\r
3f7c993d 162 qsort(statelists[0].head.keyhead, statelists[0].len, sizeof(uint64_t), compar_int);\r
163 qsort(statelists[1].head.keyhead, statelists[1].len, sizeof(uint64_t), compar_int);\r
9492e0b0 164\r
165 uint64_t *p5, *p6, *p7;\r
3f7c993d 166 p5 = p7 = statelists[0].head.keyhead; \r
167 p6 = statelists[1].head.keyhead;\r
168 while (p5 <= statelists[0].tail.keytail && p6 <= statelists[1].tail.keytail) {\r
9492e0b0 169 if (compar_int(p5, p6) == 0) {\r
170 *p7++ = *p5++;\r
171 p6++;\r
172 }\r
173 else {\r
174 while (compar_int(p5, p6) == -1) p5++;\r
175 while (compar_int(p5, p6) == 1) p6++;\r
176 }\r
177 }\r
3f7c993d 178 statelists[0].len = p7 - statelists[0].head.keyhead;\r
d294f582 179 statelists[0].tail.keytail = --p7;\r
9492e0b0 180\r
181 memset(resultKey, 0, 6);\r
d294f582 182 uint64_t key64 = 0;\r
183\r
9492e0b0 184 // The list may still contain several key candidates. Test each of them with mfCheckKeys\r
6b10e329 185 // uint32_t max_keys = keycnt > (USB_CMD_DATA_SIZE/6) ? (USB_CMD_DATA_SIZE/6) : keycnt;\r
d294f582 186\r
6b10e329 187 uint32_t numOfCandidates = statelists[0].len;\r
188 if ( numOfCandidates == 0 ) goto out;\r
189\r
190 uint8_t *keyBlock = malloc(numOfCandidates*6);\r
191 if (keyBlock == NULL) return -6;\r
192\r
193 for (i = 0; i < numOfCandidates; ++i){\r
3f7c993d 194 crypto1_get_lfsr(statelists[0].head.slhead + i, &key64);\r
6b10e329 195 num_to_bytes(key64, 6, keyBlock + i * 6);\r
196 }\r
197\r
198 if (!mfCheckKeys(statelists[0].blockNo, statelists[0].keyType, false, numOfCandidates, keyBlock, &key64)) { \r
199 free(statelists[0].head.slhead);\r
200 free(statelists[1].head.slhead);\r
201 free(keyBlock);\r
d294f582 202 num_to_bytes(key64, 6, resultKey);\r
203\r
6b10e329 204 PrintAndLog("UID: %08x target block:%3u key type: %c -- Found key [%012"llx"]",\r
205 uid,\r
206 (uint16_t)resp.arg[2] & 0xff,\r
207 (resp.arg[2] >> 8) ? 'B' : 'A',\r
208 key64\r
209 );\r
210 return -5;\r
9492e0b0 211 }\r
6b10e329 212\r
213out:\r
214 PrintAndLog("UID: %08x target block:%3u key type: %c",\r
215 uid,\r
216 (uint16_t)resp.arg[2] & 0xff,\r
217 (resp.arg[2] >> 8) ? 'B' : 'A'\r
218 ); \r
219\r
3f7c993d 220 free(statelists[0].head.slhead);\r
d294f582 221 free(statelists[1].head.slhead);\r
222 return -4;\r
f397b5cc
M
223}\r
224\r
b10a759f 225int mfCheckKeys (uint8_t blockNo, uint8_t keyType, bool clear_trace, uint8_t keycnt, uint8_t * keyBlock, uint64_t * key){\r
9492e0b0 226\r
f397b5cc 227 *key = 0;\r
d294f582 228 UsbCommand c = {CMD_MIFARE_CHKKEYS, { (blockNo | (keyType<<8)), clear_trace, keycnt}};\r
f397b5cc 229 memcpy(c.d.asBytes, keyBlock, 6 * keycnt);\r
0de8e387 230 clearCommandBuffer();\r
9492e0b0 231 SendCommand(&c);\r
902cb3c0 232 UsbCommand resp;\r
d294f582 233 if (!WaitForResponseTimeout(CMD_ACK,&resp, 3000)) return 1;\r
902cb3c0 234 if ((resp.arg[0] & 0xff) != 0x01) return 2;\r
235 *key = bytes_to_num(resp.d.asBytes, 6);\r
f397b5cc
M
236 return 0;\r
237}\r
8556b852 238\r
55acbb2a
M
239// EMULATOR\r
240\r
8556b852 241int mfEmlGetMem(uint8_t *data, int blockNum, int blocksCount) {\r
545a1f38 242 UsbCommand c = {CMD_MIFARE_EML_MEMGET, {blockNum, blocksCount, 0}};\r
0de8e387 243 clearCommandBuffer();\r
902cb3c0 244 SendCommand(&c);\r
5c065fa0 245 UsbCommand resp;\r
902cb3c0 246 if (!WaitForResponseTimeout(CMD_ACK,&resp,1500)) return 1;\r
247 memcpy(data, resp.d.asBytes, blocksCount * 16);\r
8556b852
M
248 return 0;\r
249}\r
250\r
251int mfEmlSetMem(uint8_t *data, int blockNum, int blocksCount) {\r
0de8e387 252 return mfEmlSetMem_xt(data, blockNum, blocksCount, 16);\r
253}\r
254\r
255int mfEmlSetMem_xt(uint8_t *data, int blockNum, int blocksCount, int blockBtWidth) {\r
256 UsbCommand c = {CMD_MIFARE_EML_MEMSET, {blockNum, blocksCount, blockBtWidth}};\r
257 memcpy(c.d.asBytes, data, blocksCount * blockBtWidth); \r
258\r
259 clearCommandBuffer();\r
545a1f38 260 SendCommand(&c);\r
8556b852
M
261 return 0;\r
262}\r
263\r
55acbb2a
M
264// "MAGIC" CARD\r
265\r
c2731f37 266int mfCSetUID(uint8_t *uid, uint8_t *atqa, uint8_t *sak, uint8_t *oldUID, uint8_t wipecard) {\r
3bba7dea 267\r
c2731f37 268 uint8_t params = MAGIC_SINGLE;\r
269 uint8_t block0[16];\r
270 memset(block0, 0x00, sizeof(block0));\r
c2731f37 271\r
272 int old = mfCGetBlock(0, block0, params);\r
d294f582 273 if (old == 0)\r
c2731f37 274 PrintAndLog("old block 0: %s", sprint_hex(block0, sizeof(block0)));\r
d294f582 275 else \r
276 PrintAndLog("Couldn't get old data. Will write over the last bytes of Block 0."); \r
3bba7dea
JH
277\r
278 // fill in the new values\r
279 // UID\r
280 memcpy(block0, uid, 4); \r
281 // Mifare UID BCC\r
282 block0[4] = block0[0]^block0[1]^block0[2]^block0[3];\r
283 // mifare classic SAK(byte 5) and ATQA(byte 6 and 7, reversed)\r
c2731f37 284 if ( sak != NULL )\r
3bba7dea 285 block0[5]=sak[0];\r
c2731f37 286 \r
287 if ( atqa != NULL ) {\r
3bba7dea
JH
288 block0[6]=atqa[1];\r
289 block0[7]=atqa[0];\r
80b1b53f 290 }\r
3bba7dea 291 PrintAndLog("new block 0: %s", sprint_hex(block0,16));\r
c2731f37 292 \r
293 if ( wipecard ) params |= MAGIC_WIPE; \r
294 if ( oldUID == NULL) params |= MAGIC_UID;\r
295 \r
296 return mfCSetBlock(0, block0, oldUID, params);\r
f774db95
M
297}\r
298\r
c2731f37 299int mfCSetBlock(uint8_t blockNo, uint8_t *data, uint8_t *uid, uint8_t params) {\r
0675f200 300\r
1b492a97 301 uint8_t isOK = 0;\r
c2731f37 302 UsbCommand c = {CMD_MIFARE_CSETBLOCK, {params, blockNo, 0}};\r
f774db95 303 memcpy(c.d.asBytes, data, 16); \r
0de8e387 304 clearCommandBuffer();\r
305 SendCommand(&c);\r
1b492a97 306 UsbCommand resp;\r
c2731f37 307 if (WaitForResponseTimeout(CMD_ACK, &resp, 1500)) {\r
902cb3c0 308 isOK = resp.arg[0] & 0xff;\r
80b1b53f 309 if (uid != NULL) \r
310 memcpy(uid, resp.d.asBytes, 4);\r
311 if (!isOK) \r
312 return 2;\r
0675f200
M
313 } else {\r
314 PrintAndLog("Command execute timeout");\r
315 return 1;\r
316 }\r
317 return 0;\r
318}\r
545a1f38
M
319\r
320int mfCGetBlock(uint8_t blockNo, uint8_t *data, uint8_t params) {\r
321 uint8_t isOK = 0;\r
c2731f37 322 UsbCommand c = {CMD_MIFARE_CGETBLOCK, {params, blockNo, 0}}; \r
0de8e387 323 clearCommandBuffer();\r
545a1f38 324 SendCommand(&c);\r
0de8e387 325 UsbCommand resp;\r
902cb3c0 326 if (WaitForResponseTimeout(CMD_ACK,&resp,1500)) {\r
327 isOK = resp.arg[0] & 0xff;\r
328 memcpy(data, resp.d.asBytes, 16);\r
545a1f38
M
329 if (!isOK) return 2;\r
330 } else {\r
331 PrintAndLog("Command execute timeout");\r
332 return 1;\r
333 }\r
334 return 0;\r
335}\r
55acbb2a
M
336\r
337// SNIFFER\r
338\r
e0c635d1
M
339// constants\r
340static uint8_t trailerAccessBytes[4] = {0x08, 0x77, 0x8F, 0x00};\r
341\r
55acbb2a 342// variables\r
e4691591 343char logHexFileName[FILE_PATH_SIZE] = {0x00};\r
e0c635d1 344static uint8_t traceCard[4096] = {0x00};\r
e4691591 345static char traceFileName[FILE_PATH_SIZE] = {0x00};\r
55acbb2a
M
346static int traceState = TRACE_IDLE;\r
347static uint8_t traceCurBlock = 0;\r
348static uint8_t traceCurKey = 0;\r
349\r
350struct Crypto1State *traceCrypto1 = NULL;\r
351\r
046fd766 352struct Crypto1State *revstate = NULL;\r
353\r
354uint64_t key = 0;\r
355uint32_t ks2 = 0;\r
356uint32_t ks3 = 0;\r
55acbb2a 357\r
046fd766 358uint32_t uid = 0; // serial number\r
359uint32_t nt =0; // tag challenge\r
360uint32_t nr_enc =0; // encrypted reader challenge\r
361uint32_t ar_enc =0; // encrypted reader response\r
362uint32_t at_enc =0; // encrypted tag response\r
55acbb2a 363\r
e0c635d1
M
364int isTraceCardEmpty(void) {\r
365 return ((traceCard[0] == 0) && (traceCard[1] == 0) && (traceCard[2] == 0) && (traceCard[3] == 0));\r
366}\r
55acbb2a 367\r
e0c635d1
M
368int isBlockEmpty(int blockN) {\r
369 for (int i = 0; i < 16; i++) \r
370 if (traceCard[blockN * 16 + i] != 0) return 0;\r
55acbb2a 371\r
e0c635d1
M
372 return 1;\r
373}\r
374\r
375int isBlockTrailer(int blockN) {\r
d294f582 376 return ((blockN & 0x03) == 0x03);\r
e0c635d1
M
377}\r
378\r
379int loadTraceCard(uint8_t *tuid) {\r
380 FILE * f;\r
80b1b53f 381 char buf[64] = {0x00};\r
382 uint8_t buf8[64] = {0x00};\r
e0c635d1
M
383 int i, blockNum;\r
384 \r
80b1b53f 385 if (!isTraceCardEmpty()) \r
386 saveTraceCard();\r
387 \r
55acbb2a
M
388 memset(traceCard, 0x00, 4096);\r
389 memcpy(traceCard, tuid + 3, 4);\r
80b1b53f 390\r
e0c635d1
M
391 FillFileNameByUID(traceFileName, tuid, ".eml", 7);\r
392\r
393 f = fopen(traceFileName, "r");\r
664bb5ae 394 if (!f) return 1;\r
e0c635d1
M
395 \r
396 blockNum = 0;\r
e4691591 397 \r
e0c635d1 398 while(!feof(f)){\r
e4691591 399 \r
e0c635d1 400 memset(buf, 0, sizeof(buf));\r
759c16b3 401 if (fgets(buf, sizeof(buf), f) == NULL) {\r
046fd766 402 PrintAndLog("File reading error.");\r
1b492a97 403 fclose(f);\r
759c16b3 404 return 2;\r
046fd766 405 }\r
e0c635d1
M
406\r
407 if (strlen(buf) < 32){\r
408 if (feof(f)) break;\r
409 PrintAndLog("File content error. Block data must include 32 HEX symbols");\r
1b492a97 410 fclose(f);\r
e0c635d1
M
411 return 2;\r
412 }\r
413 for (i = 0; i < 32; i += 2)\r
414 sscanf(&buf[i], "%02x", (unsigned int *)&buf8[i / 2]);\r
415\r
416 memcpy(traceCard + blockNum * 16, buf8, 16);\r
417\r
418 blockNum++;\r
419 }\r
420 fclose(f);\r
421\r
422 return 0;\r
423}\r
424\r
425int saveTraceCard(void) {\r
426 FILE * f;\r
427 \r
428 if ((!strlen(traceFileName)) || (isTraceCardEmpty())) return 0;\r
429 \r
430 f = fopen(traceFileName, "w+");\r
664bb5ae 431 if ( !f ) return 1;\r
e4691591 432 \r
e0c635d1
M
433 for (int i = 0; i < 64; i++) { // blocks\r
434 for (int j = 0; j < 16; j++) // bytes\r
435 fprintf(f, "%02x", *(traceCard + i * 16 + j)); \r
436 fprintf(f,"\n");\r
437 }\r
438 fclose(f);\r
e0c635d1
M
439 return 0;\r
440}\r
441\r
442int mfTraceInit(uint8_t *tuid, uint8_t *atqa, uint8_t sak, bool wantSaveToEmlFile) {\r
443\r
80b1b53f 444 if (traceCrypto1) \r
445 crypto1_destroy(traceCrypto1);\r
446\r
e0c635d1
M
447 traceCrypto1 = NULL;\r
448\r
80b1b53f 449 if (wantSaveToEmlFile) \r
450 loadTraceCard(tuid);\r
451 \r
55acbb2a
M
452 traceCard[4] = traceCard[0] ^ traceCard[1] ^ traceCard[2] ^ traceCard[3];\r
453 traceCard[5] = sak;\r
454 memcpy(&traceCard[6], atqa, 2);\r
455 traceCurBlock = 0;\r
456 uid = bytes_to_num(tuid + 3, 4);\r
457 \r
458 traceState = TRACE_IDLE;\r
459\r
460 return 0;\r
461}\r
462\r
463void mf_crypto1_decrypt(struct Crypto1State *pcs, uint8_t *data, int len, bool isEncrypted){\r
464 uint8_t bt = 0;\r
465 int i;\r
466 \r
467 if (len != 1) {\r
468 for (i = 0; i < len; i++)\r
469 data[i] = crypto1_byte(pcs, 0x00, isEncrypted) ^ data[i];\r
470 } else {\r
d294f582 471 bt = 0; \r
472 bt |= (crypto1_bit(pcs, 0, isEncrypted) ^ BIT(data[0], 0)) << 0;\r
473 bt |= (crypto1_bit(pcs, 0, isEncrypted) ^ BIT(data[0], 1)) << 1;\r
474 bt |= (crypto1_bit(pcs, 0, isEncrypted) ^ BIT(data[0], 2)) << 2;\r
475 bt |= (crypto1_bit(pcs, 0, isEncrypted) ^ BIT(data[0], 3)) << 3; \r
55acbb2a
M
476 data[0] = bt;\r
477 }\r
478 return;\r
479}\r
480\r
a501c82b 481int mfTraceDecode(uint8_t *data_src, int len, bool wantSaveToEmlFile) {\r
d294f582 482 \r
55acbb2a
M
483 uint8_t data[64];\r
484\r
485 if (traceState == TRACE_ERROR) return 1;\r
d294f582 486 \r
55acbb2a
M
487 if (len > 64) {\r
488 traceState = TRACE_ERROR;\r
489 return 1;\r
490 }\r
491 \r
492 memcpy(data, data_src, len);\r
493 if ((traceCrypto1) && ((traceState == TRACE_IDLE) || (traceState > TRACE_AUTH_OK))) {\r
494 mf_crypto1_decrypt(traceCrypto1, data, len, 0);\r
495 PrintAndLog("dec> %s", sprint_hex(data, len));\r
496 AddLogHex(logHexFileName, "dec> ", data, len); \r
497 }\r
498 \r
499 switch (traceState) {\r
500 case TRACE_IDLE: \r
8b6a93df
M
501 // check packet crc16!\r
502 if ((len >= 4) && (!CheckCrc14443(CRC_14443_A, data, len))) {\r
503 PrintAndLog("dec> CRC ERROR!!!");\r
504 AddLogLine(logHexFileName, "dec> ", "CRC ERROR!!!"); \r
505 traceState = TRACE_ERROR; // do not decrypt the next commands\r
506 return 1;\r
507 }\r
55acbb2a
M
508 \r
509 // AUTHENTICATION\r
a61b4976 510 if ((len == 4) && ((data[0] == 0x60) || (data[0] == 0x61))) {\r
55acbb2a
M
511 traceState = TRACE_AUTH1;\r
512 traceCurBlock = data[1];\r
513 traceCurKey = data[0] == 60 ? 1:0;\r
514 return 0;\r
515 }\r
516\r
517 // READ\r
518 if ((len ==4) && ((data[0] == 0x30))) {\r
519 traceState = TRACE_READ_DATA;\r
520 traceCurBlock = data[1];\r
521 return 0;\r
522 }\r
523\r
524 // WRITE\r
525 if ((len ==4) && ((data[0] == 0xA0))) {\r
526 traceState = TRACE_WRITE_OK;\r
527 traceCurBlock = data[1];\r
528 return 0;\r
529 }\r
530\r
531 // HALT\r
532 if ((len ==4) && ((data[0] == 0x50) && (data[1] == 0x00))) {\r
533 traceState = TRACE_ERROR; // do not decrypt the next commands\r
534 return 0;\r
535 }\r
536 \r
537 return 0;\r
538 break;\r
539 \r
540 case TRACE_READ_DATA: \r
541 if (len == 18) {\r
542 traceState = TRACE_IDLE;\r
543\r
e0c635d1
M
544 if (isBlockTrailer(traceCurBlock)) {\r
545 memcpy(traceCard + traceCurBlock * 16 + 6, data + 6, 4);\r
546 } else {\r
547 memcpy(traceCard + traceCurBlock * 16, data, 16);\r
548 }\r
549 if (wantSaveToEmlFile) saveTraceCard();\r
55acbb2a
M
550 return 0;\r
551 } else {\r
552 traceState = TRACE_ERROR;\r
553 return 1;\r
554 }\r
555 break;\r
556\r
557 case TRACE_WRITE_OK: \r
a61b4976 558 if ((len == 1) && (data[0] == 0x0a)) {\r
55acbb2a
M
559 traceState = TRACE_WRITE_DATA;\r
560\r
561 return 0;\r
562 } else {\r
563 traceState = TRACE_ERROR;\r
564 return 1;\r
565 }\r
566 break;\r
567\r
568 case TRACE_WRITE_DATA: \r
569 if (len == 18) {\r
570 traceState = TRACE_IDLE;\r
571\r
572 memcpy(traceCard + traceCurBlock * 16, data, 16);\r
e0c635d1 573 if (wantSaveToEmlFile) saveTraceCard();\r
55acbb2a
M
574 return 0;\r
575 } else {\r
576 traceState = TRACE_ERROR;\r
577 return 1;\r
578 }\r
579 break;\r
580\r
581 case TRACE_AUTH1: \r
582 if (len == 4) {\r
583 traceState = TRACE_AUTH2;\r
55acbb2a
M
584 nt = bytes_to_num(data, 4);\r
585 return 0;\r
586 } else {\r
587 traceState = TRACE_ERROR;\r
588 return 1;\r
589 }\r
590 break;\r
591\r
592 case TRACE_AUTH2: \r
593 if (len == 8) {\r
594 traceState = TRACE_AUTH_OK;\r
595\r
596 nr_enc = bytes_to_num(data, 4);\r
597 ar_enc = bytes_to_num(data + 4, 4);\r
598 return 0;\r
599 } else {\r
600 traceState = TRACE_ERROR;\r
601 return 1;\r
602 }\r
603 break;\r
604\r
605 case TRACE_AUTH_OK: \r
606 if (len ==4) {\r
607 traceState = TRACE_IDLE;\r
608\r
609 at_enc = bytes_to_num(data, 4);\r
610 \r
611 // decode key here)\r
a61b4976 612 ks2 = ar_enc ^ prng_successor(nt, 64);\r
613 ks3 = at_enc ^ prng_successor(nt, 96);\r
614 revstate = lfsr_recovery64(ks2, ks3);\r
615 lfsr_rollback_word(revstate, 0, 0);\r
616 lfsr_rollback_word(revstate, 0, 0);\r
617 lfsr_rollback_word(revstate, nr_enc, 1);\r
618 lfsr_rollback_word(revstate, uid ^ nt, 0);\r
619\r
046fd766 620 crypto1_get_lfsr(revstate, &key);\r
621 printf("Key: %012"llx"\n",key);\r
622 AddLogUint64(logHexFileName, "key: ", key); \r
55acbb2a 623 \r
e0c635d1
M
624 int blockShift = ((traceCurBlock & 0xFC) + 3) * 16;\r
625 if (isBlockEmpty((traceCurBlock & 0xFC) + 3)) memcpy(traceCard + blockShift + 6, trailerAccessBytes, 4);\r
626 \r
55acbb2a 627 if (traceCurKey) {\r
046fd766 628 num_to_bytes(key, 6, traceCard + blockShift + 10);\r
55acbb2a 629 } else {\r
046fd766 630 num_to_bytes(key, 6, traceCard + blockShift);\r
55acbb2a 631 }\r
e0c635d1 632 if (wantSaveToEmlFile) saveTraceCard();\r
55acbb2a
M
633\r
634 if (traceCrypto1) {\r
635 crypto1_destroy(traceCrypto1);\r
636 }\r
637 \r
638 // set cryptosystem state\r
639 traceCrypto1 = lfsr_recovery64(ks2, ks3);\r
640 \r
641// nt = crypto1_word(traceCrypto1, nt ^ uid, 1) ^ nt;\r
642\r
046fd766 643 /* traceCrypto1 = crypto1_create(key); // key in lfsr\r
55acbb2a
M
644 crypto1_word(traceCrypto1, nt ^ uid, 0);\r
645 crypto1_word(traceCrypto1, ar, 1);\r
646 crypto1_word(traceCrypto1, 0, 0);\r
647 crypto1_word(traceCrypto1, 0, 0);*/\r
648 \r
649 return 0;\r
650 } else {\r
651 traceState = TRACE_ERROR;\r
652 return 1;\r
653 }\r
654 break;\r
655\r
656 default: \r
657 traceState = TRACE_ERROR;\r
658 return 1;\r
659 }\r
660\r
661 return 0;\r
662}\r
e98572a1 663\r
664int tryDecryptWord(uint32_t nt, uint32_t ar_enc, uint32_t at_enc, uint8_t *data, int len){\r
665 /*\r
666 uint32_t nt; // tag challenge\r
3bc7b13d 667 uint32_t nr_enc; // encrypted reader challenge\r
e98572a1 668 uint32_t ar_enc; // encrypted reader response\r
669 uint32_t at_enc; // encrypted tag response\r
670 */\r
3bc7b13d 671 struct Crypto1State *pcs = NULL;\r
672 \r
e98572a1 673 ks2 = ar_enc ^ prng_successor(nt, 64);\r
674 ks3 = at_enc ^ prng_successor(nt, 96);\r
3bc7b13d 675 \r
676 PrintAndLog("Decrypting data with:");\r
677 PrintAndLog(" nt: %08x",nt);\r
678 PrintAndLog(" ar_enc: %08x",ar_enc);\r
679 PrintAndLog(" at_enc: %08x",at_enc);\r
680 PrintAndLog("\nEncrypted data: [%s]", sprint_hex(data,len) );\r
681\r
682 pcs = lfsr_recovery64(ks2, ks3);\r
683 mf_crypto1_decrypt(pcs, data, len, FALSE);\r
e98572a1 684 PrintAndLog("Decrypted data: [%s]", sprint_hex(data,len) );\r
3bc7b13d 685 crypto1_destroy(pcs);\r
e98572a1 686 return 0;\r
687}\r
Impressum, Datenschutz