static bool authenticate(byte uiBlock) { mp.mpa.abtAuthUid = new byte[4]; mp.mpa.abtKey = new byte[6]; mtKeys.amb = new mifare_classic_block[256]; mtKeys.amb[uiBlock] = new mifare_classic_block(); mtKeys.amb[uiBlock].mbt = new mifare_classic_block_trailer(); mtKeys.amb[uiBlock].mbt.abtKeyA = new byte[6]; mtKeys.amb[uiBlock].mbt.abtKeyB = new byte[6]; // memcpy(mp.mpa.abtAuthUid, nt.nti.nai.abtUid + nt.nti.nai.szUidLen - 4, 4); Array.Copy(nt.nti.abtUid, (int)nt.nti.szUidLen.ToUInt64() - 4, mp.mpa.abtAuthUid, 0, 4); // Should we use key A or B? mifare_cmd mc = (bUseKeyA) ? mifare_cmd.MC_AUTH_A : mifare_cmd.MC_AUTH_B; // Key file authentication. if (bUseKeyFile) { // Locate the trailer (with the keys) used for this sector uint uiTrailerBlock = get_trailer_block(uiBlock); // Extract the right key from dump file if (bUseKeyA) { Array.Copy(mtKeys.amb[uiTrailerBlock].mbt.abtKeyA, mp.mpa.abtKey, Marshal.SizeOf(mp.mpa.abtKey)); } else { Array.Copy(mtKeys.amb[uiTrailerBlock].mbt.abtKeyB, mp.mpa.abtKey, Marshal.SizeOf(mp.mpa.abtKey)); } // Try to authenticate for the current sector if (SharpFreeFare.Functions.nfc_initiator_mifare_cmd(pnd, mc, uiBlock, ref mp)) { return(true); } } // If formatting or not using key file, try to guess the right key if (bFormatCard || !bUseKeyFile) { for (int key_index = 0; key_index < num_keys; key_index++) { // memcpy(mp.mpa.abtKey, keys + (key_index * 6), 6); Array.Copy(keys, (key_index * 6), mp.mpa.abtKey, 0, 6); if (SharpFreeFare.Functions.nfc_initiator_mifare_cmd(pnd, mc, uiBlock, ref mp)) { if (bUseKeyA) { Array.Copy(mp.mpa.abtKey, mtKeys.amb[uiBlock].mbt.abtKeyA, mtKeys.amb[uiBlock].mbt.abtKeyA.Length); } else { Array.Copy(mp.mpa.abtKey, mtKeys.amb[uiBlock].mbt.abtKeyB, mtKeys.amb[uiBlock].mbt.abtKeyB.Length); } return(true); } nfc_target outTarget; if (SharpNFC.PInvoke.Functions.nfc_initiator_select_passive_target(pnd, nmMifare, nt.nti.abtUid, nt.nti.szUidLen.ToUInt32(), out outTarget) <= 0) { Console.WriteLine("ERR:tag was removed"); return(false); } } } return(false); }
nfc_initiator_mifare_cmd(NFCInternal.nfc_device pnd, mifare_cmd mc, byte ui8Block, mifare_param pmp) { byte[] abtRx = new byte[265]; byte szParamLen; byte[] abtCmd = new byte[265]; //bool bEasyFraming; abtCmd[0] = (byte)mc; // The MIFARE Classic command abtCmd[1] = ui8Block; // The block address (1K=0x00..0x39, 4K=0x00..0xff) switch (mc) { // Read and store command have no parameter case mifare_cmd.MC_READ: case mifare_cmd.MC_STORE: szParamLen = 0; break; // Authenticate command case mifare_cmd.MC_AUTH_A: case mifare_cmd.MC_AUTH_B: szParamLen = (byte)pmp.mpa.Length(); //sizeof( mifare_param_auth); if (szParamLen > 0) { pmp.mpa.FillRawData(abtCmd, 2); } break; // Data command case mifare_cmd.MC_WRITE: szParamLen = (byte)pmp.mpd.Length(); //sizeof( mifare_param_data); if (szParamLen > 0) { pmp.mpd.FillRawData(abtCmd, 2); } break; // Value command case mifare_cmd.MC_DECREMENT: case mifare_cmd.MC_INCREMENT: case mifare_cmd.MC_TRANSFER: szParamLen = (byte)pmp.mpv.Length(); //sizeof( mifare_param_value); if (szParamLen > 0) { pmp.mpv.FillRawData(abtCmd, 2); } break; // Please fix your code, you never should reach this statement default: return(false); break; } // FIXME: Save and restore bEasyFraming // bEasyFraming = nfc_device_get_property_bool (pnd, NP_EASY_FRAMING, &bEasyFraming); if (NFC.nfc_device_set_property_bool(pnd, NFC.nfc_property.NP_EASY_FRAMING, true) < 0) { //nfc_perror(pnd, "nfc_device_set_property_bool"); return(false); } // Fire the mifare command int res; if ((res = NFC.nfc_initiator_transceive_bytes(pnd, abtCmd, 2 + szParamLen, abtRx, abtRx.Length, -1)) < 0) { if (res == NFC.NFC_ERFTRANS) { // "Invalid received frame", usual means we are // authenticated on a sector but the requested MIFARE cmd (read, write) // is not permitted by current acces bytes; // So there is nothing to do here. } else { //nfc_perror(pnd, "nfc_initiator_transceive_bytes"); } // XXX nfc_device_set_property_bool (pnd, NP_EASY_FRAMING, bEasyFraming); return(false); } /* XXX * if (nfc_device_set_property_bool (pnd, NP_EASY_FRAMING, bEasyFraming) < 0) { * nfc_perror (pnd, "nfc_device_set_property_bool"); * return false; * } */ // When we have executed a read command, copy the received bytes into the param if (mc == mifare_cmd.MC_READ) { if (res == 16) { MiscTool.memcpy(pmp.mpd.abtData, 0, abtRx, 0, 16); } else { return(false); } } // Command succesfully executed return(true); }
public static bool authenticate(NFC.nfc_device pnd, NFC.nfc_target pnt, uint uiBlock, byte[] key, mifare_cmd mc) { mifare_param mp = new mifare_param(); // Set the authentication information (uid) MiscTool.memcpy(mp.mpa.abtAuthUid, 0, pnt.nti.nai.abtUid, pnt.nti.nai.szUidLen - 4, 4); MiscTool.memcpy(mp.mpa.abtKey, 0, key, 0, 6); if (nfc_initiator_mifare_cmd(pnd, mc, (byte)uiBlock, mp)) { return(true); } if (NFC.nfc_initiator_select_passive_target(pnd, nmMfClassic, pnt.nti.nai.abtUid, pnt.nti.nai.szUidLen, null) <= 0) { //ERR("tag was removed"); return(false); } return(true); }
public static extern bool nfc_initiator_mifare_cmd(IntPtr pnd, mifare_cmd mc, byte ui8Block, ref mifare_param pmp);