]> cvs.zerfleddert.de Git - proxmark3-svn/blame - client/mifarehost.c
Merge branch 'master' of https://github.com/Proxmark/proxmark3
[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
6a1f2d82 241int mfCSetUID(uint8_t *uid, uint8_t *oldUID, bool 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
6a1f2d82 254int mfCSetBlock(uint8_t blockNo, uint8_t *data, uint8_t *uid, bool 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
313uint32_t nr_enc; // encrypted reader challenge\r
314uint32_t ar_enc; // encrypted reader response\r
315uint32_t at_enc; // encrypted tag response\r
316\r
e0c635d1
M
317int isTraceCardEmpty(void) {\r
318 return ((traceCard[0] == 0) && (traceCard[1] == 0) && (traceCard[2] == 0) && (traceCard[3] == 0));\r
319}\r
55acbb2a 320\r
e0c635d1
M
321int isBlockEmpty(int blockN) {\r
322 for (int i = 0; i < 16; i++) \r
323 if (traceCard[blockN * 16 + i] != 0) return 0;\r
55acbb2a 324\r
e0c635d1
M
325 return 1;\r
326}\r
327\r
328int isBlockTrailer(int blockN) {\r
329 return ((blockN & 0x03) == 0x03);\r
330}\r
331\r
332int loadTraceCard(uint8_t *tuid) {\r
333 FILE * f;\r
334 char buf[64];\r
335 uint8_t buf8[64];\r
336 int i, blockNum;\r
337 \r
338 if (!isTraceCardEmpty()) saveTraceCard();\r
55acbb2a
M
339 memset(traceCard, 0x00, 4096);\r
340 memcpy(traceCard, tuid + 3, 4);\r
e0c635d1
M
341 FillFileNameByUID(traceFileName, tuid, ".eml", 7);\r
342\r
343 f = fopen(traceFileName, "r");\r
344 if (!f) return 1;\r
345 \r
346 blockNum = 0;\r
347 while(!feof(f)){\r
348 memset(buf, 0, sizeof(buf));\r
759c16b3 349 if (fgets(buf, sizeof(buf), f) == NULL) {\r
ca4714cd
MHS
350 PrintAndLog("File reading error.");\r
351 fclose(f);\r
759c16b3 352 return 2;\r
ca4714cd 353 }\r
e0c635d1
M
354\r
355 if (strlen(buf) < 32){\r
356 if (feof(f)) break;\r
357 PrintAndLog("File content error. Block data must include 32 HEX symbols");\r
ca4714cd 358 fclose(f);\r
e0c635d1
M
359 return 2;\r
360 }\r
361 for (i = 0; i < 32; i += 2)\r
362 sscanf(&buf[i], "%02x", (unsigned int *)&buf8[i / 2]);\r
363\r
364 memcpy(traceCard + blockNum * 16, buf8, 16);\r
365\r
366 blockNum++;\r
367 }\r
368 fclose(f);\r
369\r
370 return 0;\r
371}\r
372\r
373int saveTraceCard(void) {\r
374 FILE * f;\r
375 \r
376 if ((!strlen(traceFileName)) || (isTraceCardEmpty())) return 0;\r
377 \r
378 f = fopen(traceFileName, "w+");\r
379 for (int i = 0; i < 64; i++) { // blocks\r
380 for (int j = 0; j < 16; j++) // bytes\r
381 fprintf(f, "%02x", *(traceCard + i * 16 + j)); \r
382 fprintf(f,"\n");\r
383 }\r
384 fclose(f);\r
385\r
386 return 0;\r
387}\r
388\r
389int mfTraceInit(uint8_t *tuid, uint8_t *atqa, uint8_t sak, bool wantSaveToEmlFile) {\r
390\r
391 if (traceCrypto1) crypto1_destroy(traceCrypto1);\r
392 traceCrypto1 = NULL;\r
393\r
394 if (wantSaveToEmlFile) loadTraceCard(tuid);\r
55acbb2a
M
395 traceCard[4] = traceCard[0] ^ traceCard[1] ^ traceCard[2] ^ traceCard[3];\r
396 traceCard[5] = sak;\r
397 memcpy(&traceCard[6], atqa, 2);\r
398 traceCurBlock = 0;\r
399 uid = bytes_to_num(tuid + 3, 4);\r
400 \r
401 traceState = TRACE_IDLE;\r
402\r
403 return 0;\r
404}\r
405\r
406void mf_crypto1_decrypt(struct Crypto1State *pcs, uint8_t *data, int len, bool isEncrypted){\r
407 uint8_t bt = 0;\r
408 int i;\r
409 \r
410 if (len != 1) {\r
411 for (i = 0; i < len; i++)\r
412 data[i] = crypto1_byte(pcs, 0x00, isEncrypted) ^ data[i];\r
413 } else {\r
414 bt = 0;\r
415 for (i = 0; i < 4; i++)\r
416 bt |= (crypto1_bit(pcs, 0, isEncrypted) ^ BIT(data[0], i)) << i;\r
417 \r
418 data[0] = bt;\r
419 }\r
420 return;\r
421}\r
422\r
423\r
6a1f2d82 424int mfTraceDecode(uint8_t *data_src, int len, bool wantSaveToEmlFile) {\r
55acbb2a
M
425 uint8_t data[64];\r
426\r
427 if (traceState == TRACE_ERROR) return 1;\r
428 if (len > 64) {\r
429 traceState = TRACE_ERROR;\r
430 return 1;\r
431 }\r
432 \r
433 memcpy(data, data_src, len);\r
434 if ((traceCrypto1) && ((traceState == TRACE_IDLE) || (traceState > TRACE_AUTH_OK))) {\r
435 mf_crypto1_decrypt(traceCrypto1, data, len, 0);\r
436 PrintAndLog("dec> %s", sprint_hex(data, len));\r
437 AddLogHex(logHexFileName, "dec> ", data, len); \r
438 }\r
439 \r
440 switch (traceState) {\r
441 case TRACE_IDLE: \r
8b6a93df
M
442 // check packet crc16!\r
443 if ((len >= 4) && (!CheckCrc14443(CRC_14443_A, data, len))) {\r
444 PrintAndLog("dec> CRC ERROR!!!");\r
445 AddLogLine(logHexFileName, "dec> ", "CRC ERROR!!!"); \r
446 traceState = TRACE_ERROR; // do not decrypt the next commands\r
447 return 1;\r
448 }\r
55acbb2a
M
449 \r
450 // AUTHENTICATION\r
451 if ((len ==4) && ((data[0] == 0x60) || (data[0] == 0x61))) {\r
452 traceState = TRACE_AUTH1;\r
453 traceCurBlock = data[1];\r
454 traceCurKey = data[0] == 60 ? 1:0;\r
455 return 0;\r
456 }\r
457\r
458 // READ\r
459 if ((len ==4) && ((data[0] == 0x30))) {\r
460 traceState = TRACE_READ_DATA;\r
461 traceCurBlock = data[1];\r
462 return 0;\r
463 }\r
464\r
465 // WRITE\r
466 if ((len ==4) && ((data[0] == 0xA0))) {\r
467 traceState = TRACE_WRITE_OK;\r
468 traceCurBlock = data[1];\r
469 return 0;\r
470 }\r
471\r
472 // HALT\r
473 if ((len ==4) && ((data[0] == 0x50) && (data[1] == 0x00))) {\r
474 traceState = TRACE_ERROR; // do not decrypt the next commands\r
475 return 0;\r
476 }\r
477 \r
478 return 0;\r
479 break;\r
480 \r
481 case TRACE_READ_DATA: \r
482 if (len == 18) {\r
483 traceState = TRACE_IDLE;\r
484\r
e0c635d1
M
485 if (isBlockTrailer(traceCurBlock)) {\r
486 memcpy(traceCard + traceCurBlock * 16 + 6, data + 6, 4);\r
487 } else {\r
488 memcpy(traceCard + traceCurBlock * 16, data, 16);\r
489 }\r
490 if (wantSaveToEmlFile) saveTraceCard();\r
55acbb2a
M
491 return 0;\r
492 } else {\r
493 traceState = TRACE_ERROR;\r
494 return 1;\r
495 }\r
496 break;\r
497\r
498 case TRACE_WRITE_OK: \r
97d582a6 499 if ((len == 1) && (data[0] == 0x0a)) {\r
55acbb2a
M
500 traceState = TRACE_WRITE_DATA;\r
501\r
502 return 0;\r
503 } else {\r
504 traceState = TRACE_ERROR;\r
505 return 1;\r
506 }\r
507 break;\r
508\r
509 case TRACE_WRITE_DATA: \r
510 if (len == 18) {\r
511 traceState = TRACE_IDLE;\r
512\r
513 memcpy(traceCard + traceCurBlock * 16, data, 16);\r
e0c635d1 514 if (wantSaveToEmlFile) saveTraceCard();\r
55acbb2a
M
515 return 0;\r
516 } else {\r
517 traceState = TRACE_ERROR;\r
518 return 1;\r
519 }\r
520 break;\r
521\r
522 case TRACE_AUTH1: \r
523 if (len == 4) {\r
524 traceState = TRACE_AUTH2;\r
525\r
526 nt = bytes_to_num(data, 4);\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_AUTH2: \r
535 if (len == 8) {\r
536 traceState = TRACE_AUTH_OK;\r
537\r
538 nr_enc = bytes_to_num(data, 4);\r
539 ar_enc = bytes_to_num(data + 4, 4);\r
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_AUTH_OK: \r
548 if (len ==4) {\r
549 traceState = TRACE_IDLE;\r
550\r
551 at_enc = bytes_to_num(data, 4);\r
552 \r
553 // decode key here)\r
97d582a6
MHS
554 ks2 = ar_enc ^ prng_successor(nt, 64);\r
555 ks3 = at_enc ^ prng_successor(nt, 96);\r
556 revstate = lfsr_recovery64(ks2, ks3);\r
557 lfsr_rollback_word(revstate, 0, 0);\r
558 lfsr_rollback_word(revstate, 0, 0);\r
559 lfsr_rollback_word(revstate, nr_enc, 1);\r
560 lfsr_rollback_word(revstate, uid ^ nt, 0);\r
55acbb2a
M
561 crypto1_get_lfsr(revstate, &lfsr);\r
562 printf("key> %x%x\n", (unsigned int)((lfsr & 0xFFFFFFFF00000000) >> 32), (unsigned int)(lfsr & 0xFFFFFFFF));\r
563 AddLogUint64(logHexFileName, "key> ", lfsr); \r
564 \r
e0c635d1
M
565 int blockShift = ((traceCurBlock & 0xFC) + 3) * 16;\r
566 if (isBlockEmpty((traceCurBlock & 0xFC) + 3)) memcpy(traceCard + blockShift + 6, trailerAccessBytes, 4);\r
567 \r
55acbb2a 568 if (traceCurKey) {\r
e0c635d1 569 num_to_bytes(lfsr, 6, traceCard + blockShift + 10);\r
55acbb2a 570 } else {\r
e0c635d1 571 num_to_bytes(lfsr, 6, traceCard + blockShift);\r
55acbb2a 572 }\r
e0c635d1 573 if (wantSaveToEmlFile) saveTraceCard();\r
55acbb2a
M
574\r
575 if (traceCrypto1) {\r
576 crypto1_destroy(traceCrypto1);\r
577 }\r
578 \r
579 // set cryptosystem state\r
580 traceCrypto1 = lfsr_recovery64(ks2, ks3);\r
581 \r
582// nt = crypto1_word(traceCrypto1, nt ^ uid, 1) ^ nt;\r
583\r
584 /* traceCrypto1 = crypto1_create(lfsr); // key in lfsr\r
585 crypto1_word(traceCrypto1, nt ^ uid, 0);\r
586 crypto1_word(traceCrypto1, ar, 1);\r
587 crypto1_word(traceCrypto1, 0, 0);\r
588 crypto1_word(traceCrypto1, 0, 0);*/\r
589 \r
590 return 0;\r
591 } else {\r
592 traceState = TRACE_ERROR;\r
593 return 1;\r
594 }\r
595 break;\r
596\r
597 default: \r
598 traceState = TRACE_ERROR;\r
599 return 1;\r
600 }\r
601\r
602 return 0;\r
603}\r
Impressum, Datenschutz