- if(pState == NULL){
- if(nts_attacked < STATE_SIZE )
- {
- //Initialize a new state
- pState = &states[nts_attacked++];
- //Clear it before use
- memset(pState, 0, sizeof(AttackState));
- memcpy(pState->nt, nt, 4);
- i = nts_attacked;
- //Dbprintf("New state created, nt=");
- }else if(!high_entropy_warning_issued){
- /**
- *If we wound up here, it means that the state table was eaten up by potential nonces. This could be fixed by
- *increasing the size of the state buffer, however, it points to some other problem. Ideally, we should get the same nonce
- *every time. Realistically we should get a few different nonces, but if we get more than 50, there is probably somehting
- *else that is wrong. An attack using too high nonce entropy will take **LONG** time to finish.
- */
- DbpString("WARNING: Nonce entropy is suspiciously high, something is wrong. Check timeouts (and perhaps increase STATE_SIZE)");
- high_entropy_warning_issued = 1;
- }
- }
- if(pState == NULL) continue;
-
- int result = continueAttack(pState, receivedAnswer);
-
- if(result == 1){
- //One state progressed another step
- if(pState->nt_diff > progress)
- {
- progress = pState->nt_diff;
- //Alert the user
- Dbprintf("Recovery progress: %d/8, NTs attacked: %d ", progress,nts_attacked );
- }
- //Dbprintf("State increased to %d in state %d", pState->nt_diff, i);
- }
- else if(result == 2){
- //Dbprintf("Continue attack no answer, par is now %d", pState->par);
- }
- else if(result == 0){
- reportResults(uid,pState,1);
- return;
- }
- }
- reportResults(uid,NULL,0);
+ // CodeIso14443aBitsAsReaderPar(mf_auth, sizeof(mf_auth)*8, GetParity(mf_auth, sizeof(mf_auth)*8));
+
+ sync_time = (sync_time & 0xfffffff8) + sync_cycles + catch_up_cycles;
+ catch_up_cycles = 0;
+
+ // if we missed the sync time already, advance to the next nonce repeat
+ while(GetCountMifare() > sync_time) {
+ sync_time = (sync_time & 0xfffffff8) + sync_cycles;
+ }
+
+ // Transmit MIFARE_CLASSIC_AUTH at synctime. Should result in returning the same tag nonce (== nt_attacked)
+ ReaderTransmit(mf_auth, sizeof(mf_auth), &sync_time);
+
+ // Receive the (4 Byte) "random" nonce
+ if (!ReaderReceive(receivedAnswer)) {
+ if (MF_DBGLEVEL >= 1) Dbprintf("Mifare: Couldn't receive tag nonce");
+ continue;
+ }
+
+ previous_nt = nt;
+ nt = bytes_to_num(receivedAnswer, 4);
+
+ // Transmit reader nonce with fake par
+ ReaderTransmitPar(mf_nr_ar, sizeof(mf_nr_ar), par, NULL);
+
+ if (first_try && previous_nt && !nt_attacked) { // we didn't calibrate our clock yet
+ int nt_distance = dist_nt(previous_nt, nt);
+ if (nt_distance == 0) {
+ nt_attacked = nt;
+ }
+ else {
+ if (nt_distance == -99999) { // invalid nonce received, try again
+ continue;
+ }
+ sync_cycles = (sync_cycles - nt_distance);
+ if (MF_DBGLEVEL >= 3) Dbprintf("calibrating in cycle %d. nt_distance=%d, Sync_cycles: %d\n", i, nt_distance, sync_cycles);
+ continue;
+ }
+ }
+
+ if ((nt != nt_attacked) && nt_attacked) { // we somehow lost sync. Try to catch up again...
+ catch_up_cycles = -dist_nt(nt_attacked, nt);
+ if (catch_up_cycles == 99999) { // invalid nonce received. Don't resync on that one.
+ catch_up_cycles = 0;
+ continue;
+ }
+ if (catch_up_cycles == last_catch_up) {
+ consecutive_resyncs++;
+ }
+ else {
+ last_catch_up = catch_up_cycles;
+ consecutive_resyncs = 0;
+ }
+ if (consecutive_resyncs < 3) {
+ if (MF_DBGLEVEL >= 3) Dbprintf("Lost sync in cycle %d. nt_distance=%d. Consecutive Resyncs = %d. Trying one time catch up...\n", i, -catch_up_cycles, consecutive_resyncs);
+ }
+ else {
+ sync_cycles = sync_cycles + catch_up_cycles;
+ if (MF_DBGLEVEL >= 3) Dbprintf("Lost sync in cycle %d for the fourth time consecutively (nt_distance = %d). Adjusting sync_cycles to %d.\n", i, -catch_up_cycles, sync_cycles);
+ }
+ continue;
+ }
+
+ consecutive_resyncs = 0;
+
+ // Receive answer. This will be a 4 Bit NACK when the 8 parity bits are OK after decoding
+ if (ReaderReceive(receivedAnswer))
+ {
+ catch_up_cycles = 8; // the PRNG is delayed by 8 cycles due to the NAC (4Bits = 0x05 encrypted) transfer
+
+ if (nt_diff == 0)
+ {
+ par_low = par & 0x07; // there is no need to check all parities for other nt_diff. Parity Bits for mf_nr_ar[0..2] won't change
+ }
+
+ led_on = !led_on;
+ if(led_on) LED_B_ON(); else LED_B_OFF();
+
+ par_list[nt_diff] = par;
+ ks_list[nt_diff] = receivedAnswer[0] ^ 0x05;
+
+ // Test if the information is complete
+ if (nt_diff == 0x07) {
+ isOK = 1;
+ break;
+ }
+
+ nt_diff = (nt_diff + 1) & 0x07;
+ mf_nr_ar[3] = (mf_nr_ar[3] & 0x1F) | (nt_diff << 5);
+ par = par_low;
+ } else {
+ if (nt_diff == 0 && first_try)
+ {
+ par++;
+ } else {
+ par = (((par >> 3) + 1) << 3) | par_low;
+ }
+ }
+ }
+
+ LogTrace((const uint8_t *)&nt, 4, 0, GetParity((const uint8_t *)&nt, 4), TRUE);
+ LogTrace(par_list, 8, 0, GetParity(par_list, 8), TRUE);
+ LogTrace(ks_list, 8, 0, GetParity(ks_list, 8), TRUE);
+
+ mf_nr_ar[3] &= 0x1F;
+
+ byte_t buf[28];
+ memcpy(buf + 0, uid, 4);
+ num_to_bytes(nt, 4, buf + 4);
+ memcpy(buf + 8, par_list, 8);
+ memcpy(buf + 16, ks_list, 8);
+ memcpy(buf + 24, mf_nr_ar, 4);
+
+ cmd_send(CMD_ACK,isOK,0,0,buf,28);
+
+ // Thats it...
+ FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);
+ LEDsoff();
+ tracing = TRUE;