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