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