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