- if(mifare_classic_authex(pcs, cuid, blockNo, keyType, ui64Key, AUTH_NESTED, &nt2)) {\r
- if (MF_DBGLEVEL >= 1) Dbprintf("Auth2 error");\r
- break;\r
- };\r
- \r
- nttmp = prng_successor(nt1, 500);\r
- for (i = 501; i < 2000; i++) {\r
- nttmp = prng_successor(nttmp, 1);\r
- if (nttmp == nt2) break;\r
+ // prepare next select. No need to power down the card.\r
+ if(mifare_classic_halt(pcs, cuid)) {\r
+ if (MF_DBGLEVEL >= 1) Dbprintf("Nested: Halt error");\r
+ rtr--;\r
+ continue;\r
+ }\r
+\r
+ if(!iso14443a_select_card(uid, NULL, &cuid)) {\r
+ if (MF_DBGLEVEL >= 1) Dbprintf("Nested: Can't select card");\r
+ rtr--;\r
+ continue;\r
+ };\r
+\r
+ auth1_time = 0;\r
+ if(mifare_classic_authex(pcs, cuid, blockNo, keyType, ui64Key, AUTH_FIRST, &nt1, &auth1_time)) {\r
+ if (MF_DBGLEVEL >= 1) Dbprintf("Nested: Auth1 error");\r
+ rtr--;\r
+ continue;\r
+ };\r
+\r
+ if (delta_time) {\r
+ auth2_time = auth1_time + delta_time;\r
+ } else {\r
+ auth2_time = 0;\r
+ }\r
+ if(mifare_classic_authex(pcs, cuid, blockNo, keyType, ui64Key, AUTH_NESTED, &nt2, &auth2_time)) {\r
+ if (MF_DBGLEVEL >= 1) Dbprintf("Nested: Auth2 error");\r
+ rtr--;\r
+ continue;\r
+ };\r
+\r
+ nttmp = prng_successor(nt1, 100); //NXP Mifare is typical around 840,but for some unlicensed/compatible mifare card this can be 160\r
+ for (i = 101; i < 1200; i++) {\r
+ nttmp = prng_successor(nttmp, 1);\r
+ if (nttmp == nt2) break;\r
+ }\r
+\r
+ if (i != 1200) {\r
+ if (rtr != 0) {\r
+ davg += i;\r
+ dmin = MIN(dmin, i);\r
+ dmax = MAX(dmax, i);\r
+ }\r
+ else {\r
+ delta_time = auth2_time - auth1_time + 32; // allow some slack for proper timing\r
+ }\r
+ if (MF_DBGLEVEL >= 3) Dbprintf("Nested: calibrating... ntdist=%d", i);\r
+ }\r