+ clear_trace();\r
+ set_tracing(true);\r
+ \r
+ if(!iso14443a_select_card(NULL, NULL, NULL, true, 0)) {\r
+ if (MF_DBGLEVEL >= 1) Dbprintf("Can't select card");\r
+ OnError(0);\r
+ return;\r
+ };\r
+\r
+ blockdata[0] = pwd[7];\r
+ blockdata[1] = pwd[6];\r
+ blockdata[2] = pwd[5];\r
+ blockdata[3] = pwd[4];\r
+ if(mifare_ultra_writeblock( 44, blockdata)) {\r
+ if (MF_DBGLEVEL >= 1) Dbprintf("Write block error");\r
+ OnError(44);\r
+ return;\r
+ };\r
+\r
+ blockdata[0] = pwd[3];\r
+ blockdata[1] = pwd[2];\r
+ blockdata[2] = pwd[1];\r
+ blockdata[3] = pwd[0];\r
+ if(mifare_ultra_writeblock( 45, blockdata)) {\r
+ if (MF_DBGLEVEL >= 1) Dbprintf("Write block error");\r
+ OnError(45);\r
+ return;\r
+ };\r
+\r
+ blockdata[0] = pwd[15];\r
+ blockdata[1] = pwd[14];\r
+ blockdata[2] = pwd[13];\r
+ blockdata[3] = pwd[12];\r
+ if(mifare_ultra_writeblock( 46, blockdata)) {\r
+ if (MF_DBGLEVEL >= 1) Dbprintf("Write block error");\r
+ OnError(46);\r
+ return;\r
+ };\r
+\r
+ blockdata[0] = pwd[11];\r
+ blockdata[1] = pwd[10];\r
+ blockdata[2] = pwd[9];\r
+ blockdata[3] = pwd[8];\r
+ if(mifare_ultra_writeblock( 47, blockdata)) {\r
+ if (MF_DBGLEVEL >= 1) Dbprintf("Write block error");\r
+ OnError(47);\r
+ return;\r
+ }; \r
+\r
+ if(mifare_ultra_halt()) {\r
+ if (MF_DBGLEVEL >= 1) Dbprintf("Halt error");\r
+ OnError(0);\r
+ return;\r
+ };\r
+\r
+ cmd_send(CMD_ACK,1,0,0,0,0);\r
+ FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);\r
+ LEDsoff();\r
+ set_tracing(FALSE);\r
+}\r
+\r
+// Return 1 if the nonce is invalid else return 0\r
+int valid_nonce(uint32_t Nt, uint32_t NtEnc, uint32_t Ks1, uint8_t *parity) {\r
+ return ((oddparity8((Nt >> 24) & 0xFF) == ((parity[0]) ^ oddparity8((NtEnc >> 24) & 0xFF) ^ BIT(Ks1,16))) & \\r
+ (oddparity8((Nt >> 16) & 0xFF) == ((parity[1]) ^ oddparity8((NtEnc >> 16) & 0xFF) ^ BIT(Ks1,8))) & \\r
+ (oddparity8((Nt >> 8) & 0xFF) == ((parity[2]) ^ oddparity8((NtEnc >> 8) & 0xFF) ^ BIT(Ks1,0)))) ? 1 : 0;\r
+}\r
+\r
+\r
+//-----------------------------------------------------------------------------\r
+// acquire encrypted nonces in order to perform the attack described in\r
+// Carlo Meijer, Roel Verdult, "Ciphertext-only Cryptanalysis on Hardened\r
+// Mifare Classic Cards" in Proceedings of the 22nd ACM SIGSAC Conference on \r
+// Computer and Communications Security, 2015\r
+//-----------------------------------------------------------------------------\r
+#define AUTHENTICATION_TIMEOUT 848 //848 // card times out 1ms after wrong authentication (according to NXP documentation)\r
+#define PRE_AUTHENTICATION_LEADTIME 400 // some (non standard) cards need a pause after select before they are ready for first authentication \r
+\r
+void MifareAcquireEncryptedNonces(uint32_t arg0, uint32_t arg1, uint32_t flags, uint8_t *datain)\r
+{\r
+ uint64_t ui64Key = 0;\r
+ uint8_t uid[10] = {0x00};\r
+ uint32_t cuid = 0;\r
+ uint8_t cascade_levels = 0;\r
+ struct Crypto1State mpcs = {0, 0};\r
+ struct Crypto1State *pcs;\r
+ pcs = &mpcs;\r
+ uint8_t receivedAnswer[MAX_MIFARE_FRAME_SIZE] = {0x00};\r
+ int16_t isOK = 0;\r
+ uint8_t par_enc[1] = {0x00};\r
+ uint8_t nt_par_enc = 0;\r
+ uint8_t buf[USB_CMD_DATA_SIZE] = {0x00};\r
+ uint32_t timeout = 0;\r
+ \r
+ uint8_t blockNo = arg0 & 0xff;\r
+ uint8_t keyType = (arg0 >> 8) & 0xff;\r
+ uint8_t targetBlockNo = arg1 & 0xff;\r
+ uint8_t targetKeyType = (arg1 >> 8) & 0xff;\r
+ ui64Key = bytes_to_num(datain, 6);\r
+ bool initialize = flags & 0x0001;\r
+ bool slow = flags & 0x0002;\r
+ bool field_off = flags & 0x0004;\r
+ \r