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