]> git.zerfleddert.de Git - proxmark3-svn/blame - client/mifarehost.c
Minor typo
[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
M
298static uint8_t traceCard[4096] = {0x00};\r
299static char traceFileName[20];\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
353 PrintAndLog("File reading error.");\r
354 return 2;\r
355 }\r
e0c635d1
M
356\r
357 if (strlen(buf) < 32){\r
358 if (feof(f)) break;\r
359 PrintAndLog("File content error. Block data must include 32 HEX symbols");\r
360 return 2;\r
361 }\r
362 for (i = 0; i < 32; i += 2)\r
363 sscanf(&buf[i], "%02x", (unsigned int *)&buf8[i / 2]);\r
364\r
365 memcpy(traceCard + blockNum * 16, buf8, 16);\r
366\r
367 blockNum++;\r
368 }\r
369 fclose(f);\r
370\r
371 return 0;\r
372}\r
373\r
374int saveTraceCard(void) {\r
375 FILE * f;\r
376 \r
377 if ((!strlen(traceFileName)) || (isTraceCardEmpty())) return 0;\r
378 \r
379 f = fopen(traceFileName, "w+");\r
380 for (int i = 0; i < 64; i++) { // blocks\r
381 for (int j = 0; j < 16; j++) // bytes\r
382 fprintf(f, "%02x", *(traceCard + i * 16 + j)); \r
383 fprintf(f,"\n");\r
384 }\r
385 fclose(f);\r
386\r
387 return 0;\r
388}\r
389\r
390int mfTraceInit(uint8_t *tuid, uint8_t *atqa, uint8_t sak, bool wantSaveToEmlFile) {\r
391\r
392 if (traceCrypto1) crypto1_destroy(traceCrypto1);\r
393 traceCrypto1 = NULL;\r
394\r
395 if (wantSaveToEmlFile) loadTraceCard(tuid);\r
55acbb2a
M
396 traceCard[4] = traceCard[0] ^ traceCard[1] ^ traceCard[2] ^ traceCard[3];\r
397 traceCard[5] = sak;\r
398 memcpy(&traceCard[6], atqa, 2);\r
399 traceCurBlock = 0;\r
400 uid = bytes_to_num(tuid + 3, 4);\r
401 \r
402 traceState = TRACE_IDLE;\r
403\r
404 return 0;\r
405}\r
406\r
407void mf_crypto1_decrypt(struct Crypto1State *pcs, uint8_t *data, int len, bool isEncrypted){\r
408 uint8_t bt = 0;\r
409 int i;\r
410 \r
411 if (len != 1) {\r
412 for (i = 0; i < len; i++)\r
413 data[i] = crypto1_byte(pcs, 0x00, isEncrypted) ^ data[i];\r
414 } else {\r
415 bt = 0;\r
416 for (i = 0; i < 4; i++)\r
417 bt |= (crypto1_bit(pcs, 0, isEncrypted) ^ BIT(data[0], i)) << i;\r
418 \r
419 data[0] = bt;\r
420 }\r
421 return;\r
422}\r
423\r
424\r
71d90e54 425int mfTraceDecode(uint8_t *data_src, int len, uint32_t parity, bool wantSaveToEmlFile) {\r
55acbb2a
M
426 uint8_t data[64];\r
427\r
428 if (traceState == TRACE_ERROR) return 1;\r
429 if (len > 64) {\r
430 traceState = TRACE_ERROR;\r
431 return 1;\r
432 }\r
433 \r
434 memcpy(data, data_src, len);\r
435 if ((traceCrypto1) && ((traceState == TRACE_IDLE) || (traceState > TRACE_AUTH_OK))) {\r
436 mf_crypto1_decrypt(traceCrypto1, data, len, 0);\r
437 PrintAndLog("dec> %s", sprint_hex(data, len));\r
438 AddLogHex(logHexFileName, "dec> ", data, len); \r
439 }\r
440 \r
441 switch (traceState) {\r
442 case TRACE_IDLE: \r
8b6a93df
M
443 // check packet crc16!\r
444 if ((len >= 4) && (!CheckCrc14443(CRC_14443_A, data, len))) {\r
445 PrintAndLog("dec> CRC ERROR!!!");\r
446 AddLogLine(logHexFileName, "dec> ", "CRC ERROR!!!"); \r
447 traceState = TRACE_ERROR; // do not decrypt the next commands\r
448 return 1;\r
449 }\r
55acbb2a
M
450 \r
451 // AUTHENTICATION\r
452 if ((len ==4) && ((data[0] == 0x60) || (data[0] == 0x61))) {\r
453 traceState = TRACE_AUTH1;\r
454 traceCurBlock = data[1];\r
455 traceCurKey = data[0] == 60 ? 1:0;\r
456 return 0;\r
457 }\r
458\r
459 // READ\r
460 if ((len ==4) && ((data[0] == 0x30))) {\r
461 traceState = TRACE_READ_DATA;\r
462 traceCurBlock = data[1];\r
463 return 0;\r
464 }\r
465\r
466 // WRITE\r
467 if ((len ==4) && ((data[0] == 0xA0))) {\r
468 traceState = TRACE_WRITE_OK;\r
469 traceCurBlock = data[1];\r
470 return 0;\r
471 }\r
472\r
473 // HALT\r
474 if ((len ==4) && ((data[0] == 0x50) && (data[1] == 0x00))) {\r
475 traceState = TRACE_ERROR; // do not decrypt the next commands\r
476 return 0;\r
477 }\r
478 \r
479 return 0;\r
480 break;\r
481 \r
482 case TRACE_READ_DATA: \r
483 if (len == 18) {\r
484 traceState = TRACE_IDLE;\r
485\r
e0c635d1
M
486 if (isBlockTrailer(traceCurBlock)) {\r
487 memcpy(traceCard + traceCurBlock * 16 + 6, data + 6, 4);\r
488 } else {\r
489 memcpy(traceCard + traceCurBlock * 16, data, 16);\r
490 }\r
491 if (wantSaveToEmlFile) saveTraceCard();\r
55acbb2a
M
492 return 0;\r
493 } else {\r
494 traceState = TRACE_ERROR;\r
495 return 1;\r
496 }\r
497 break;\r
498\r
499 case TRACE_WRITE_OK: \r
500 if ((len == 1) && (data[0] = 0x0a)) {\r
501 traceState = TRACE_WRITE_DATA;\r
502\r
503 return 0;\r
504 } else {\r
505 traceState = TRACE_ERROR;\r
506 return 1;\r
507 }\r
508 break;\r
509\r
510 case TRACE_WRITE_DATA: \r
511 if (len == 18) {\r
512 traceState = TRACE_IDLE;\r
513\r
514 memcpy(traceCard + traceCurBlock * 16, data, 16);\r
e0c635d1 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_AUTH1: \r
524 if (len == 4) {\r
525 traceState = TRACE_AUTH2;\r
526\r
527 nt = bytes_to_num(data, 4);\r
71d90e54 528 nt_par = parity;\r
55acbb2a
M
529 return 0;\r
530 } else {\r
531 traceState = TRACE_ERROR;\r
532 return 1;\r
533 }\r
534 break;\r
535\r
536 case TRACE_AUTH2: \r
537 if (len == 8) {\r
538 traceState = TRACE_AUTH_OK;\r
539\r
540 nr_enc = bytes_to_num(data, 4);\r
541 ar_enc = bytes_to_num(data + 4, 4);\r
71d90e54 542 nr_ar_par = parity;\r
55acbb2a
M
543 return 0;\r
544 } else {\r
545 traceState = TRACE_ERROR;\r
546 return 1;\r
547 }\r
548 break;\r
549\r
550 case TRACE_AUTH_OK: \r
551 if (len ==4) {\r
552 traceState = TRACE_IDLE;\r
553\r
554 at_enc = bytes_to_num(data, 4);\r
71d90e54 555 at_par = parity;\r
55acbb2a
M
556 \r
557 // decode key here)\r
558 if (!traceCrypto1) {\r
559 ks2 = ar_enc ^ prng_successor(nt, 64);\r
560 ks3 = at_enc ^ prng_successor(nt, 96);\r
561 revstate = lfsr_recovery64(ks2, ks3);\r
562 lfsr_rollback_word(revstate, 0, 0);\r
563 lfsr_rollback_word(revstate, 0, 0);\r
564 lfsr_rollback_word(revstate, nr_enc, 1);\r
565 lfsr_rollback_word(revstate, uid ^ nt, 0);\r
566 }else{\r
567 ks2 = ar_enc ^ prng_successor(nt, 64);\r
568 ks3 = at_enc ^ prng_successor(nt, 96);\r
569 revstate = lfsr_recovery64(ks2, ks3);\r
570 lfsr_rollback_word(revstate, 0, 0);\r
571 lfsr_rollback_word(revstate, 0, 0);\r
572 lfsr_rollback_word(revstate, nr_enc, 1);\r
573 lfsr_rollback_word(revstate, uid ^ nt, 0);\r
574 }\r
575 crypto1_get_lfsr(revstate, &lfsr);\r
576 printf("key> %x%x\n", (unsigned int)((lfsr & 0xFFFFFFFF00000000) >> 32), (unsigned int)(lfsr & 0xFFFFFFFF));\r
577 AddLogUint64(logHexFileName, "key> ", lfsr); \r
578 \r
e0c635d1
M
579 int blockShift = ((traceCurBlock & 0xFC) + 3) * 16;\r
580 if (isBlockEmpty((traceCurBlock & 0xFC) + 3)) memcpy(traceCard + blockShift + 6, trailerAccessBytes, 4);\r
581 \r
55acbb2a 582 if (traceCurKey) {\r
e0c635d1 583 num_to_bytes(lfsr, 6, traceCard + blockShift + 10);\r
55acbb2a 584 } else {\r
e0c635d1 585 num_to_bytes(lfsr, 6, traceCard + blockShift);\r
55acbb2a 586 }\r
e0c635d1 587 if (wantSaveToEmlFile) saveTraceCard();\r
55acbb2a
M
588\r
589 if (traceCrypto1) {\r
590 crypto1_destroy(traceCrypto1);\r
591 }\r
592 \r
593 // set cryptosystem state\r
594 traceCrypto1 = lfsr_recovery64(ks2, ks3);\r
595 \r
596// nt = crypto1_word(traceCrypto1, nt ^ uid, 1) ^ nt;\r
597\r
598 /* traceCrypto1 = crypto1_create(lfsr); // key in lfsr\r
599 crypto1_word(traceCrypto1, nt ^ uid, 0);\r
600 crypto1_word(traceCrypto1, ar, 1);\r
601 crypto1_word(traceCrypto1, 0, 0);\r
602 crypto1_word(traceCrypto1, 0, 0);*/\r
603 \r
604 return 0;\r
605 } else {\r
606 traceState = TRACE_ERROR;\r
607 return 1;\r
608 }\r
609 break;\r
610\r
611 default: \r
612 traceState = TRACE_ERROR;\r
613 return 1;\r
614 }\r
615\r
616 return 0;\r
617}\r
Impressum, Datenschutz