+ if (isBlockTrailer(traceCurBlock)) {\r
+ memcpy(traceCard + traceCurBlock * 16 + 6, data + 6, 4);\r
+ } else {\r
+ memcpy(traceCard + traceCurBlock * 16, data, 16);\r
+ }\r
+ if (wantSaveToEmlFile) saveTraceCard();\r
+ return 0;\r
+ } else {\r
+ traceState = TRACE_ERROR;\r
+ return 1;\r
+ }\r
+ break;\r
+ case TRACE_WRITE_OK: \r
+ if ((len == 1) && (data[0] == 0x0a)) {\r
+ traceState = TRACE_WRITE_DATA;\r
+ return 0;\r
+ } else {\r
+ traceState = TRACE_ERROR;\r
+ return 1;\r
+ }\r
+ break;\r
+ case TRACE_WRITE_DATA: \r
+ if (len == 18) {\r
+ traceState = TRACE_IDLE;\r
+ memcpy(traceCard + traceCurBlock * 16, data, 16);\r
+ if (wantSaveToEmlFile) saveTraceCard();\r
+ return 0;\r
+ } else {\r
+ traceState = TRACE_ERROR;\r
+ return 1;\r
+ }\r
+ break;\r
+ case TRACE_AUTH1: \r
+ if (len == 4) {\r
+ traceState = TRACE_AUTH2;\r
+ nt = bytes_to_num(data, 4);\r
+ return 0;\r
+ } else {\r
+ traceState = TRACE_ERROR;\r
+ return 1;\r
+ }\r
+ break;\r
+ case TRACE_AUTH2: \r
+ if (len == 8) {\r
+ traceState = TRACE_AUTH_OK;\r
+ nr_enc = bytes_to_num(data, 4);\r
+ ar_enc = bytes_to_num(data + 4, 4);\r
+ return 0;\r
+ } else {\r
+ traceState = TRACE_ERROR;\r
+ return 1;\r
+ }\r
+ break;\r
+ case TRACE_AUTH_OK: \r
+ if (len == 4) {\r
+ traceState = TRACE_IDLE;\r
+ at_enc = bytes_to_num(data, 4);\r
+ \r
+ // decode key here)\r
+ ks2 = ar_enc ^ prng_successor(nt, 64);\r
+ ks3 = at_enc ^ prng_successor(nt, 96);\r
+ revstate = lfsr_recovery64(ks2, ks3);\r
+ lfsr_rollback_word(revstate, 0, 0);\r
+ lfsr_rollback_word(revstate, 0, 0);\r
+ lfsr_rollback_word(revstate, nr_enc, 1);\r
+ lfsr_rollback_word(revstate, cuid ^ nt, 0);\r
+ crypto1_get_lfsr(revstate, &key);\r
+ PrintAndLog("Found Key: [%012"llx"]", key);\r
+ \r
+ //if ( tryMfk64(cuid, nt, nr_enc, ar_enc, at_enc, &key) )\r
+ AddLogUint64(logHexFileName, "Found Key: ", key); \r
+ \r
+ int blockShift = ((traceCurBlock & 0xFC) + 3) * 16;\r
+ if (isBlockEmpty((traceCurBlock & 0xFC) + 3)) memcpy(traceCard + blockShift + 6, trailerAccessBytes, 4);\r
+ \r
+ if (traceCurKey)\r
+ num_to_bytes(key, 6, traceCard + blockShift + 10);\r
+ else\r
+ num_to_bytes(key, 6, traceCard + blockShift);\r
+ \r
+ if (wantSaveToEmlFile)\r
+ saveTraceCard();\r