/** * @brief try to find a valid tag * @return pointer on a valid tag or NULL. */ nfc_target* ned_select_tag(nfc_device* nfc_device, nfc_target* tag) { nfc_target* rv = malloc( sizeof(nfc_target) ); nfc_modulation nm = { .nmt = NMT_ISO14443A, .nbr = NBR_106 }; if ( tag == NULL ) { // We are looking for any tag. // Poll for a ISO14443A (MIFARE) tag if ( nfc_initiator_select_passive_target ( nfc_device, nm, NULL, 0, rv ) < 0 ) { free (rv); rv = NULL; } } else { // tag is not NULL, we are looking for specific tag // debug_print_tag(tag); if ( nfc_initiator_select_passive_target ( nfc_device, tag->nm, tag->nti.nai.abtUid, tag->nti.nai.szUidLen, rv ) < 0 ) { free (rv); rv = NULL; } } if (rv != NULL) { nfc_initiator_deselect_target ( nfc_device ); } return rv; }
static bool write_card (void) { uint32_t uiBlock = 0; bool bFailure = false; uint32_t uiWritenPages = 0; uint32_t uiSkippedPages; char buffer[BUFSIZ]; bool write_otp; bool write_lock; printf ("Write OTP bytes ? [yN] "); fgets (buffer, BUFSIZ, stdin); write_otp = ((buffer[0] == 'y') || (buffer[0] == 'Y')); printf ("Write Lock bytes ? [yN] "); fgets (buffer, BUFSIZ, stdin); write_lock = ((buffer[0] == 'y') || (buffer[0] == 'Y')); printf ("Writing %d pages |", uiBlocks + 1); /* We need to skip 2 first pages. */ printf ("ss"); uiSkippedPages = 2; for (int page = 0x2; page <= 0xF; page++) { if ((page==0x2) && (!write_lock)) { printf ("s"); uiSkippedPages++; continue; } if ((page==0x3) && (!write_otp)) { printf ("s"); uiSkippedPages++; continue; } // Show if the readout went well if (bFailure) { // When a failure occured we need to redo the anti-collision if (!nfc_initiator_select_passive_target (pnd, nmMifare, NULL, 0, &nt)) { ERR ("tag was removed"); return false; } bFailure = false; } // For the Mifare Ultralight, this write command can be used // in compatibility mode, which only actually writes the first // page (4 bytes). The Ultralight-specific Write command only // writes one page at a time. uiBlock = page / 4; memcpy (mp.mpd.abtData, mtDump.amb[uiBlock].mbd.abtData + ((page % 4) * 4), 16); if (!nfc_initiator_mifare_cmd (pnd, MC_WRITE, page, &mp)) bFailure = true; print_success_or_failure (bFailure, &uiWritenPages); } printf ("|\n"); printf ("Done, %d of %d pages written (%d pages skipped).\n", uiWritenPages, uiBlocks + 1, uiSkippedPages); return true; }
static int get_rats(void) { int res; uint8_t abtRats[2] = { 0xe0, 0x50}; // Use raw send/receive methods if (nfc_device_set_property_bool(pnd, NP_EASY_FRAMING, false) < 0) { nfc_perror(pnd, "nfc_configure"); return -1; } res = nfc_initiator_transceive_bytes(pnd, abtRats, sizeof(abtRats), abtRx, sizeof(abtRx), 0); if (res > 0) { // ISO14443-4 card, turn RF field off/on to access ISO14443-3 again if (nfc_device_set_property_bool(pnd, NP_ACTIVATE_FIELD, false) < 0) { nfc_perror(pnd, "nfc_configure"); return -1; } if (nfc_device_set_property_bool(pnd, NP_ACTIVATE_FIELD, true) < 0) { nfc_perror(pnd, "nfc_configure"); return -1; } } // Reselect tag if (nfc_initiator_select_passive_target(pnd, nmMifare, NULL, 0, &nt) <= 0) { printf("Error: tag disappeared\n"); nfc_close(pnd); nfc_exit(context); exit(EXIT_FAILURE); } return res; }
int get_rats(void) { int res; uint8_t abtRats[2] = { 0xe0, 0x50}; //! Use raw send/receive methods if (nfc_device_set_property_bool(pnd, NP_EASY_FRAMING, false) < 0) { nfc_perror(pnd, "nfc_configure"); nfc_strerror_r(pnd, message_erreur, TAILLE_MESSAGE_ERREUR); return -1; } res = nfc_initiator_transceive_bytes(pnd, abtRats, sizeof(abtRats), abtRx, sizeof(abtRx), 0); if (res > 0) { //! ISO14443-4 card, turn RF field off/on to access ISO14443-3 again nfc_device_set_property_bool(pnd, NP_ACTIVATE_FIELD, false); nfc_device_set_property_bool(pnd, NP_ACTIVATE_FIELD, true); } //! Reselect tag if (nfc_initiator_select_passive_target(pnd, nmMifare, NULL, 0, &nt) <= 0) { sprintf(message_erreur,"Error: tag disappeared !"); nfc_close(pnd); nfc_exit(context); return EXIT_FAILURE; //! Échec ! } return res; }
/* * call-seq: * select(tag) * * Select the +tag+ type from the device */ static VALUE dev_select(VALUE self, VALUE tag) { nfc_device * dev; nfc_modulation * mod; nfc_target * ti; Data_Get_Struct(self, nfc_device, dev); Data_Get_Struct(tag, nfc_modulation, mod); ti = (nfc_target *)calloc(1, sizeof(nfc_target)); if (nfc_initiator_select_passive_target(dev, *mod, NULL, 0, ti) ) { switch(mod->nmt) { case NMT_ISO14443A: return Data_Wrap_Struct(cNfcISO14443A, 0, free, ti); break; case NMT_FELICA: return Data_Wrap_Struct(cNfcFelica, 0, free, ti); break; default: rb_raise(rb_eRuntimeError, "untested type: %d", mod->nmt); } } return Qfalse; }
int main(int argc, const char *argv[]) { nfc_device *pnd; nfc_target nt; // Allocate only a pointer to nfc_context nfc_context *context; // Initialize libnfc and set the nfc_context nfc_init(&context); // Display libnfc version const char *acLibnfcVersion = nfc_version(); printf("%s uses libnfc %s\n", argv[0], acLibnfcVersion); // Open, using the first available NFC device which can be in order of selection: // - default device specified using environment variable or // - first specified device in libnfc.conf (/etc/nfc) or // - first specified device in device-configuration directory (/etc/nfc/devices.d) or // - first auto-detected (if feature is not disabled in libnfc.conf) device pnd = nfc_open(context, NULL); if (pnd == NULL) { warnx("ERROR: %s", "Unable to open NFC device."); return EXIT_FAILURE; } // Set opened NFC device to initiator mode if (nfc_initiator_init(pnd) < 0) { nfc_perror(pnd, "nfc_initiator_init"); exit(EXIT_FAILURE); } printf("NFC reader: %s opened\n", nfc_device_get_name(pnd)); // Poll for a ISO14443A (MIFARE) tag const nfc_modulation nmMifare = { .nmt = NMT_ISO14443A, .nbr = NBR_106, }; if (nfc_initiator_select_passive_target(pnd, nmMifare, NULL, 0, &nt) > 0) { printf("The following (NFC) ISO14443A tag was found:\n"); printf(" ATQA (SENS_RES): "); print_hex(nt.nti.nai.abtAtqa, 2); printf(" UID (NFCID%c): ", (nt.nti.nai.abtUid[0] == 0x08 ? '3' : '1')); print_hex(nt.nti.nai.abtUid, nt.nti.nai.szUidLen); printf(" SAK (SEL_RES): "); print_hex(&nt.nti.nai.btSak, 1); if (nt.nti.nai.szAtsLen) { printf(" ATS (ATR): "); print_hex(nt.nti.nai.abtAts, nt.nti.nai.szAtsLen); } } // Close NFC device nfc_close(pnd); // Release the context nfc_exit(context); return EXIT_SUCCESS; }
static bool parse_card() { e.logcount = 0; e.current_tran = 0; int32_t iBlock; bool bFailure = false; //printf("Reading out %d blocks\n", uiBlocks + 1); // Read the card from end to begin for (iBlock = uiBlocks; iBlock >= 0; iBlock--) { // Authenticate everytime we reach a trailer block if(is_debug) printf(" 0x%02x : ", iBlock); if (iBlock / 4 == DO_NOT_ACCESS) { if(is_debug) printf("!\n"); continue; } if (is_trailer_block(iBlock)) { if (bFailure) { // When a failure occured we need to redo the anti-collision if (nfc_initiator_select_passive_target(pnd, nmMifare, NULL, 0, &nt) <= 0) { printf("!\nError: tag was removed\n"); return false; } bFailure = false; } fflush(stdout); // Try to authenticate for the current sector if (!authenticate(iBlock, false)) { printf("!\nError: authentication failed for block 0x%02x\n", iBlock); return false; } // Try to read out the trailer if (nfc_initiator_mifare_cmd(pnd, MC_READ, iBlock, &mp)) { if(is_debug) print_hex(mp.mpd.abtData, 16); parserights(&e, uiBlocks / 4, mp.mpd.abtData); } else { printf("!\nfailed to read trailer block 0x%02x\n", iBlock); bFailure = true; } } else { // Make sure a earlier readout did not fail if (!bFailure) { // Try to read out the data block if (nfc_initiator_mifare_cmd(pnd, MC_READ, iBlock, &mp)) { if(is_debug) print_hex(mp.mpd.abtData, 16); parseTag(&e, iBlock, mp.mpd.abtData); } else { printf("!\nError: unable to read block 0x%02x\n", iBlock); bFailure = true; } } } if ( bFailure ) return false; } fflush(stdout); return true; }
void TagInit(ReaderTag *rt){ nfc_init(&rt->context); if (rt->context == NULL) { ERR("Unable to init libnfc (malloc)"); exit(EXIT_FAILURE); } // Try to open the NFC device rt->device = nfc_open(rt->context, NULL); if (rt->device == NULL) { ERR("Error opening NFC device"); nfc_exit(rt->context); exit(EXIT_FAILURE); } if (nfc_initiator_init(rt->device) < 0) { nfc_perror(rt->device, "nfc_initiator_init"); nfc_close(rt->device); nfc_exit(rt->context); exit(EXIT_FAILURE); } // Scan until the cows come home, or the tag, which ever comes first. if (nfc_device_set_property_bool(rt->device, NP_INFINITE_SELECT, true) < 0) { nfc_perror(rt->device, "nfc_device_set_property_bool"); nfc_close(rt->device); nfc_exit(rt->context); exit(EXIT_FAILURE); } printf("NFC device: %s opened\n", nfc_device_get_name(rt->device)); printf("\nWaiting for tag\n"); // Try to find a MIFARE Ultralight tag if (nfc_initiator_select_passive_target(rt->device, nmMifare, NULL, 0, &rt->nt) <= 0) { ERR("no tag was found\n"); nfc_close(rt->device); nfc_exit(rt->context); exit(EXIT_FAILURE); } // Test if we are dealing with a MIFARE compatible tag if (rt->nt.nti.nai.abtAtqa[1] != 0x44) { ERR("tag is not a MIFARE Ultralight card\n"); nfc_close(rt->device); nfc_exit(rt->context); exit(EXIT_FAILURE); } // Get the info from the current tag printf("Found MIFARE Ultralight card with UID: "); size_t szPos; for (szPos = 0; szPos < rt->nt.nti.nai.szUidLen; szPos++) { printf("%02x", rt->nt.nti.nai.abtUid[szPos]); } printf("\n"); }
bool mf_select_target() { if (nfc_initiator_select_passive_target(device, mf_nfc_modulation, NULL, // init data 0, // init data len &target) < 0) { return false; } return true; }
int main(int argc, const char *argv[]) { nfc_device *pnd; nfc_target nt; nfc_context *context; nfc_init(&context); if (context == NULL) { printf("Unable to init libnfc (malloc)\n"); exit(EXIT_FAILURE); } const char *acLibnfcVersion = nfc_version(); (void)argc; printf("%s uses libnfc %s\n", argv[0], acLibnfcVersion); pnd = nfc_open(context, NULL); if (pnd == NULL) { printf("ERROR: %s\n", "Unable to open NFC device."); exit(EXIT_FAILURE); } if (nfc_initiator_init(pnd) < 0) { nfc_perror(pnd, "nfc_initiator_init"); exit(EXIT_FAILURE); } printf("NFC reader: %s opened\n", nfc_device_get_name(pnd)); for( ; ; ){ const nfc_modulation nmMifare = { .nmt = NMT_ISO14443A, .nbr = NBR_106, }; if (nfc_initiator_select_passive_target(pnd, nmMifare, NULL, 0, &nt) > 0) { printf("The following (NFC) ISO14443A tag was found:\n"); printf(" ATQA (SENS_RES): "); print_hex(nt.nti.nai.abtAtqa, 2); printf(" UID (NFCID%c): ", (nt.nti.nai.abtUid[0] == 0x08 ? '3' : '1')); print_hex(nt.nti.nai.abtUid, nt.nti.nai.szUidLen); printf(" SAK (SEL_RES): "); print_hex(&nt.nti.nai.btSak, 1); if (nt.nti.nai.szAtsLen) { printf(" ATS (ATR): "); print_hex(nt.nti.nai.abtAts, nt.nti.nai.szAtsLen); } sleep(3); } } nfc_close(pnd); nfc_exit(context); exit(EXIT_SUCCESS); }
int main(int argc, const char *argv[]) { system("clear"); nfc_device *pnd; nfc_target nt; // Allocate only a pointer to nfc_context nfc_context *context; // Initialize libnfc and set the nfc_context nfc_init(&context); // Display libnfc version const char *acLibnfcVersion = nfc_version(); printf("%s uses libnfc %s\n", argv[0], acLibnfcVersion); // Open, using the first available NFC device. pnd = nfc_open(context, NULL); if (pnd == NULL) { warnx("ERROR: %s", "Unable to open NFC device."); return EXIT_FAILURE; } // Set opened NFC device to initiator mode if (nfc_initiator_init(pnd) < 0) { nfc_perror(pnd, "nfc_initiator_init"); exit(EXIT_FAILURE); } printf("NFC reader: %s opened\n", nfc_device_get_name(pnd)); printf("...\n", nfc_device_get_name(pnd)); while (true){ // Poll for a ISO14443A (MIFARE) tag const nfc_modulation nmMifare = { .nmt = NMT_ISO14443A, .nbr = NBR_106, }; if (nfc_initiator_select_passive_target(pnd, nmMifare, NULL, 0, &nt) > 0) { print_hex(nt.nti.nai.abtUid, nt.nti.nai.szUidLen); } sleep(1); } // Close NFC device nfc_close(pnd); // Release the context nfc_exit(context); return EXIT_SUCCESS; }
bool authenticate(uint32_t uiBlock) { mifare_cmd mc; uint32_t uiTrailerBlock; //! Set the authentication information (uid) memcpy(mp.mpa.abtAuthUid, nt.nti.nai.abtUid + nt.nti.nai.szUidLen - 4, 4); //! Should we use key A or B? mc = (bUseKeyA) ? MC_AUTH_A : MC_AUTH_B; //! Key file authentication. if (bUseKeyFile) { //! Locate the trailer (with the keys) used for this sector uiTrailerBlock = get_trailer_block(uiBlock); //! Extract the right key from dump file if (bUseKeyA) { memcpy(mp.mpa.abtKey, mtKeys.amb[uiTrailerBlock].mbt.abtKeyA, 6); } else { memcpy(mp.mpa.abtKey, mtKeys.amb[uiTrailerBlock].mbt.abtKeyB, 6); } //! Try to authenticate for the current sector if (nfc_initiator_mifare_cmd(pnd, mc, uiBlock, &mp)) { return true; } } else { //! Try to guess the right key size_t key_index; for (key_index = 0; key_index < num_keys; key_index++) { memcpy(mp.mpa.abtKey, keys + (key_index * 6), 6); if (nfc_initiator_mifare_cmd(pnd, mc, uiBlock, &mp)) { if (bUseKeyA) { memcpy(mtKeys.amb[uiBlock].mbt.abtKeyA, &mp.mpa.abtKey, 6); } else { memcpy(mtKeys.amb[uiBlock].mbt.abtKeyB, &mp.mpa.abtKey, 6); } return true; } nfc_initiator_select_passive_target(pnd, nmMifare, nt.nti.nai.abtUid, nt.nti.nai.szUidLen, NULL); } } return false; }
bool write_card(ReaderTag rt,mifareul_tag * tag){ uint32_t uiBlock = 0; bool bFailure = false; uint32_t uiWritenPages = 0; uint32_t uiSkippedPages; char buffer[BUFSIZ]; bool write_otp=0; bool write_lock=0; uiSkippedPages = 2; for (int page = 0x2; page <= 30; page++) { if ((page == 0x2) && (!write_lock)) { printf("s"); uiSkippedPages++; continue; } if ((page == 0x3) && (!write_otp)) { printf("s"); uiSkippedPages++; continue; } // Show if the readout went well if (bFailure) { // When a failure occured we need to redo the anti-collision if (nfc_initiator_select_passive_target(rt.device, nmMifare, NULL, 0, &rt.nt) <= 0) { ERR("tag was removed"); return false; } bFailure = false; } // For the Mifare Ultralight, this write command can be used // in compatibility mode, which only actually writes the first // page (4 bytes). The Ultralight-specific Write command only // writes one page at a time. uiBlock = page / 4; memcpy(mp.mpd.abtData, tag->amb[uiBlock].mbd.abtData + ((page % 4) * 4), 16); if (!nfc_initiator_mifare_cmd(rt.device, MC_WRITE, page, &mp)) bFailure = true; print_success_or_failure(bFailure, &uiWritenPages); } printf("|\n"); printf("Done, %d of 16 pages written (%d pages skipped).\n", uiWritenPages, uiSkippedPages); return true; }
int main (int argc, const char *argv[]) { nfc_device *pnd; nfc_target nt; nfc_init (NULL); // Display libnfc version const char *acLibnfcVersion = nfc_version (); printf ("%s uses libnfc %s\n", argv[0], acLibnfcVersion); // Open, using the first available NFC device pnd = nfc_open (NULL, NULL); if (pnd == NULL) { ERR ("%s", "Unable to open NFC device."); return EXIT_FAILURE; } // Set opened NFC device to initiator mode if (nfc_initiator_init (pnd) < 0) { nfc_perror (pnd, "nfc_initiator_init"); exit (EXIT_FAILURE); } printf ("NFC reader: %s opened\n", nfc_device_get_name (pnd)); // Poll for a ISO14443A (MIFARE) tag const nfc_modulation nmMifare = { .nmt = NMT_ISO14443A, .nbr = NBR_106, }; if (nfc_initiator_select_passive_target (pnd, nmMifare, NULL, 0, &nt) > 0) { printf ("The following (NFC) ISO14443A tag was found:\n"); printf (" ATQA (SENS_RES): "); print_hex (nt.nti.nai.abtAtqa, 2); printf (" UID (NFCID%c): ", (nt.nti.nai.abtUid[0] == 0x08 ? '3' : '1')); print_hex (nt.nti.nai.abtUid, nt.nti.nai.szUidLen); printf (" SAK (SEL_RES): "); print_hex (&nt.nti.nai.btSak, 1); if (nt.nti.nai.szAtsLen) { printf (" ATS (ATR): "); print_hex (nt.nti.nai.abtAts, nt.nti.nai.szAtsLen); } } // Close NFC device nfc_close (pnd); nfc_exit (NULL); return EXIT_SUCCESS; }
int main(int argc, const char *argv[]) { nfc_device *pnd; nfc_target nt; // Allocate only a pointer to nfc_context nfc_context *context; // Initialize libnfc and set the nfc_context nfc_init(&context); if (context == NULL) { exit(EXIT_FAILURE); } // Display libnfc version const char *acLibnfcVersion = nfc_version(); (void)argc; // Open, using the first available NFC device which can be in order of selection: // - default device specified using environment variable or // - first specified device in libnfc.conf (/etc/nfc) or // - first specified device in device-configuration directory (/etc/nfc/devices.d) or // - first auto-detected (if feature is not disabled in libnfc.conf) device pnd = nfc_open(context, NULL); if (pnd == NULL) { exit(EXIT_FAILURE); } // Set opened NFC device to initiator mode if (nfc_initiator_init(pnd) < 0) { nfc_perror(pnd, "nfc_initiator_init"); exit(EXIT_FAILURE); } // Poll for a ISO14443A (MIFARE) tag nfc_modulation nmMifare; nmMifare.nmt = NMT_ISO14443A; nmMifare.nbr = NBR_106; if (nfc_initiator_select_passive_target(pnd, nmMifare, NULL, 0, &nt) > 0) print_hex(nt.nti.nai.abtUid, nt.nti.nai.szUidLen); // Close NFC device nfc_close(pnd); // Release the context nfc_exit(context); exit(EXIT_SUCCESS); }
static bool authenticate(uint32_t uiBlock) { mifare_cmd mc; uint32_t uiTrailerBlock; // Set the authentication information (uid) memcpy(mp.mpa.abtAuthUid, nt.nti.nai.abtUid + nt.nti.nai.szUidLen - 4, 4); // Should we use key A or B? mc = (bUseKeyA) ? MC_AUTH_A : MC_AUTH_B; // Key file authentication. if (bUseKeyFile) { // Locate the trailer (with the keys) used for this sector uiTrailerBlock = get_trailer_block(uiBlock); // Extract the right key from dump file if (bUseKeyA) memcpy(mp.mpa.abtKey, mtKeys.amb[uiTrailerBlock].mbt.abtKeyA, 6); else memcpy(mp.mpa.abtKey, mtKeys.amb[uiTrailerBlock].mbt.abtKeyB, 6); // Try to authenticate for the current sector if (nfc_initiator_mifare_cmd(pnd, mc, uiBlock, &mp)) return true; } // If formatting or not using key file, try to guess the right key if (bFormatCard || !bUseKeyFile) { for (size_t key_index = 0; key_index < num_keys; key_index++) { memcpy(mp.mpa.abtKey, keys + (key_index * 6), 6); if (nfc_initiator_mifare_cmd(pnd, mc, uiBlock, &mp)) { if (bUseKeyA) memcpy(mtKeys.amb[uiBlock].mbt.abtKeyA, &mp.mpa.abtKey, 6); else memcpy(mtKeys.amb[uiBlock].mbt.abtKeyB, &mp.mpa.abtKey, 6); return true; } if (nfc_initiator_select_passive_target(pnd, nmMifare, nt.nti.nai.abtUid, nt.nti.nai.szUidLen, NULL) <= 0) { ERR("tag was removed"); return false; } } } return false; }
int main(int argc, const char *argv[]) { pinMode(LED_R, OUTPUT); // Set PWM LED as PWM output pinMode(LED_G, OUTPUT); // Set regular LED as output pinMode(LED_B, OUTPUT); pinMode(DOOR, OUTPUT); //tür öffner // nfc initiiere nfc_init(&context); if (context == NULL) { printf("ERROR: fehler beim dev init\n"); exit(EXIT_FAILURE); } while (true) { doorCode = 0; // öffnet verbindung zum token pnd = nfc_open(context, NULL); led(1,1,0); //Gelb an if (pnd == NULL) { printf("ERROR: fehler beim öffnen.\n"); exit(EXIT_FAILURE); } if (nfc_initiator_init(pnd) < 0) { nfc_perror(pnd, "nfc_initiator_init"); exit(EXIT_FAILURE); } const nfc_modulation nmMifare = { .nmt = NMT_ISO14443A, .nbr = NBR_106, }; if (nfc_initiator_select_passive_target(pnd, nmMifare, NULL, 0, &nt) > 0) { toTokenKey(nt.nti.nai.abtUid); printf("%s\n",tokenKey); checkToken(); } // TODO checken ob offen ist oder nicht um dann blau oder black zu zeigen nfc_close(pnd); } nfc_exit(context); exit(EXIT_SUCCESS); }
static PyObject * my_nfc_getid() //PyObject *self, PyObject *args) { // Poll for a ISO14443A (MIFARE) tag const nfc_modulation nmMifare = { .nmt = NMT_ISO14443A, .nbr = NBR_106, }; int x = nfc_initiator_select_passive_target(pnd, nmMifare, NULL, 0, &nt); if (x > 0) { return Py_BuildValue("s", get_hex(nt.nti.nai.abtUid, nt.nti.nai.szUidLen)); } return Py_BuildValue(""); }
static bool check_magic() { bool bFailure = false; int uid_data; for (uint32_t page = 0; page <= 1; page++) { // Show if the readout went well if (bFailure) { // When a failure occured we need to redo the anti-collision if (nfc_initiator_select_passive_target(pnd, nmMifare, NULL, 0, &nt) <= 0) { ERR("tag was removed"); return false; } bFailure = false; } uid_data = 0x00000000; memcpy(mp.mpd.abtData, &uid_data, sizeof uid_data); memset(mp.mpd.abtData + 4, 0, 12); //Force the write without checking for errors - otherwise the writes to the sector 0 seem to complain nfc_initiator_mifare_cmd(pnd, MC_WRITE, page, &mp); } //Check that the ID is now set to 0x000000000000 if (nfc_initiator_mifare_cmd(pnd, MC_READ, 0, &mp)) { //printf("%u", mp.mpd.abtData); bool result = true; for (int i = 0; i <= 7; i++) { if (mp.mpd.abtData[i] != 0x00) result = false; } if (result) { return true; } } //Initially check if we can unlock via the MF method if (unlock_card()) { return true; } else { return false; } }
bool NFCReaderUnit::connect() { if (isConnected()) { LOG(LogLevel::ERRORS) << EXCEPTION_MSG_CONNECTED; disconnect(); } bool connected = d_chip_connected = false; if (d_insertedChip && d_chips.find(d_insertedChip) != d_chips.end()) { if (d_chips[d_insertedChip].nm.nmt == NMT_ISO14443A) { nfc_target pnti; nfc_modulation modulation; modulation.nmt = NMT_ISO14443A; modulation.nbr = NBR_106; // prevent infinite wait. Setting this in connectToReader() did not work // for unkown reason. nfc_safe_call(nfc_device_set_property_bool, d_device, NP_INFINITE_SELECT, false); int ret = nfc_initiator_select_passive_target(d_device, modulation, d_chips[d_insertedChip].nti.nai.abtUid, d_chips[d_insertedChip].nti.nai.szUidLen, &pnti); if (ret > 0) { LOG(DEBUGS) << "Selected passive target."; d_chip_connected = connected = true; d_insertedChip->setChipIdentifier(getCardSerialNumber(d_chips[d_insertedChip])); } else if (ret == 0) { LOG(DEBUGS) << "No target found when selecting passive NFC target."; } else { LOG(ERRORS) << "NFC Error: " << nfc_strerror(d_device); } } } return connected; }
int main(int argc, const char *argv[]) { nfc_device *pnd; nfc_target nt; nfc_context *context; nfc_init(&context); if(context == NULL) { printf("unable to init (malloc)\n"); exit(EXIT_FAILURE); } pnd = nfc_open(context, NULL); if (pnd == NULL) { printf("ERROR: %s\n", "Unable to open NFC Device."); exit(EXIT_FAILURE); } if (nfc_initiator_init(pnd) < 0) { nfc_perror(pnd, "nfc_initiator_init"); exit(EXIT_FAILURE); } printf("NFC reader: %s opened\n", nfc_device_get_name(pnd)); const nfc_modulation nmMifare = { .nmt = NMT_ISO14443A, .nbr = NBR_106, }; for(int i = 0; i < 24; i++) { if (nfc_initiator_select_passive_target(pnd, nmMifare, NULL, 0, &nt) > 0) { printf("The following (NFC) card was found:\n"); print_card(nt.nti.nai.abtUid, nt.nti.nai.szUidLen); } while (0 == nfc_initiator_target_is_present(pnd, NULL)){} } nfc_close(pnd); nfc_exit(context); exit(EXIT_SUCCESS); }
/* * Establish connection to the provided tag. */ int mifare_desfire_connect (MifareTag tag) { ASSERT_INACTIVE (tag); ASSERT_MIFARE_DESFIRE (tag); nfc_target_info_t pnti; if (nfc_initiator_select_passive_target (tag->device, NM_ISO14443A_106, tag->info.abtUid, 7, &pnti)) { tag->active = 1; free (MIFARE_DESFIRE (tag)->session_key); MIFARE_DESFIRE (tag)->session_key = NULL; MIFARE_DESFIRE (tag)->last_picc_error = OPERATION_OK; MIFARE_DESFIRE (tag)->last_pcd_error = NULL; MIFARE_DESFIRE (tag)->authenticated_key_no = NOT_YET_AUTHENTICATED; } else { errno = EIO; return -1; } return 0; }
static bool authenticate(uint32_t uiBlock, bool isTypeA) { mifare_cmd mc; // Set the authentication information (uid) memcpy(mp.mpa.abtAuthUid, nt.nti.nai.abtUid + nt.nti.nai.szUidLen - 4, 4); // Should we use key A or B? mc = (isTypeA)?MC_AUTH_A:MC_AUTH_B; int uiSecs = (uiBlocks + 1) / 4; if(isTypeA) memcpy(mp.mpa.abtKey, keysA[uiSecs - uiBlock / 4 - 1], 6); else memcpy(mp.mpa.abtKey, keysB[uiSecs - uiBlock / 4 - 1], 6); if (nfc_initiator_mifare_cmd(pnd, mc, uiBlock, &mp)) return true; nfc_initiator_select_passive_target(pnd, nmMifare, nt.nti.nai.abtUid, nt.nti.nai.szUidLen, NULL); return false; }
/* * Callback for freefare_tag_new to test presence of a MIFARE UltralightC on the reader. */ bool is_mifare_ultralightc_on_reader (nfc_device *device, nfc_iso14443a_info nai) { int ret; uint8_t cmd_step1[2]; uint8_t res_step1[9]; cmd_step1[0] = 0x1A; cmd_step1[1] = 0x00; nfc_target pnti; nfc_modulation modulation = { .nmt = NMT_ISO14443A, .nbr = NBR_106 }; nfc_initiator_select_passive_target (device, modulation, nai.abtUid, nai.szUidLen, &pnti); nfc_device_set_property_bool (device, NP_EASY_FRAMING, false); ret = nfc_initiator_transceive_bytes (device, cmd_step1, sizeof (cmd_step1), res_step1, sizeof(res_step1), 0); nfc_device_set_property_bool (device, NP_EASY_FRAMING, true); nfc_initiator_deselect_target (device); return ret >= 0; }
/* * Establish connection to the provided tag. */ int mifare_ultralight_connect (MifareTag tag) { ASSERT_INACTIVE (tag); ASSERT_MIFARE_ULTRALIGHT (tag); nfc_target pnti; nfc_modulation modulation = { .nmt = NMT_ISO14443A, .nbr = NBR_106 }; if (nfc_initiator_select_passive_target (tag->device, modulation, tag->info.abtUid, tag->info.szUidLen, &pnti) >= 0) { tag->active = 1; for (int i = 0; i < MIFARE_ULTRALIGHT_MAX_PAGE_COUNT; i++) MIFARE_ULTRALIGHT(tag)->cached_pages[i] = 0; } else { errno = EIO; return -1; } return 0; }
void tag_get_uid(nfc_device* nfc_device, nfc_target* tag, char **dest) { debug_print_tag(tag); /// @TODO We don't need to reselect tag to get his UID: nfc_target contains this data. // Poll for a ISO14443A (MIFARE) tag if ( nfc_initiator_select_passive_target ( nfc_device, tag->nm, tag->nti.nai.abtUid, tag->nti.nai.szUidLen, tag ) ) { *dest = malloc(tag->nti.nai.szUidLen*sizeof(char)*2+1); size_t szPos; char *pcUid = *dest; for (szPos=0; szPos < tag->nti.nai.szUidLen; szPos++) { sprintf(pcUid, "%02x",tag->nti.nai.abtUid[szPos]); pcUid += 2; } pcUid[0]='\0'; DBG( "ISO14443A tag found: UID=0x%s", *dest ); nfc_initiator_deselect_target ( nfc_device ); } else { *dest = NULL; DBG("%s", "ISO14443A (MIFARE) tag not found" ); return; } }
bool mf_authenticate(size_t block, const uint8_t* key, mf_key_type_t key_type) { mifare_param mp; // Set the authentication information (uid) memcpy(mp.mpa.abtAuthUid, target.nti.nai.abtUid + target.nti.nai.szUidLen - 4, 4); // Select key for authentication mifare_cmd mc = (key_type == MF_KEY_A) ? MC_AUTH_A : MC_AUTH_B; // Set the key memcpy(mp.mpa.abtKey, key, 6); // Try to authenticate for the current sector if (nfc_initiator_mifare_cmd(device, mc, (uint8_t)block, &mp)) return true; // Do the hand shaking again if auth failed nfc_initiator_select_passive_target(device, mf_nfc_modulation, NULL, 0, &target); return false; }
bool silvia_nfc_card_monitor::wait_for_card(silvia_nfc_card** card) { assert(card != NULL); if (device == NULL) { device = nfc_open(context, NULL); if (device == NULL) { return false; } nfc_initiator_init(device); } // CAVEAT: only tested with PN533 const nfc_modulation modulation = { NMT_ISO14443A, NBR_106 }; // Poll for a card int rv = 0; nfc_target target; while ((rv = nfc_initiator_select_passive_target(device, modulation, NULL, 0, &target)) == 0) { usleep(10000); } if (rv < 0) { return false; } *card = new silvia_nfc_card(device, target, "NFC reader"); return true; }
int main(int argc, const char *argv[]) { (void) argc; (void) argv; nfc_context *context; nfc_init(&context); if (context == NULL) { ERR("Unable to init libnfc (malloc)"); exit(EXIT_FAILURE); } // Display libnfc version const char *acLibnfcVersion = nfc_version(); printf("%s uses libnfc %s\n", argv[0], acLibnfcVersion); // Open using the first available NFC device nfc_device *pnd; pnd = nfc_open(context, NULL); if (pnd == NULL) { ERR("%s", "Unable to open NFC device."); nfc_exit(context); exit(EXIT_FAILURE); } printf("NFC device: %s opened\n", nfc_device_get_name(pnd)); // Print the example's menu printf("\nSelect the communication mode:\n"); printf("[1] Virtual card mode.\n"); printf("[2] Wired card mode.\n"); printf("[3] Dual card mode.\n"); printf(">> "); // Take user's choice int input = getchar(); printf("\n"); if ((input < '1') || (input > '3')) { ERR("%s", "Invalid selection."); nfc_close(pnd); nfc_exit(context); exit(EXIT_FAILURE); } /* * '1' -> "Virtual mode" (0x02) * '2' -> "Wired card" (0x03) * '3' -> "Dual card" (0x04) */ int iMode = input - '0' + 0x01; pn532_sam_mode mode = iMode; // Connect with the SAM switch (mode) { case PSM_VIRTUAL_CARD: { // FIXME Its a private pn53x function if (pn532_SAMConfiguration(pnd, mode, 0) < 0) { nfc_perror(pnd, "pn53x_SAMConfiguration"); nfc_close(pnd); nfc_exit(context); exit(EXIT_FAILURE); } printf("Now the SAM is readable for 1 minute from an external reader.\n"); wait_one_minute(); } break; case PSM_WIRED_CARD: { // Set opened NFC device to initiator mode if (nfc_initiator_init_secure_element(pnd) < 0) { nfc_perror(pnd, "nfc_initiator_init_secure_element"); nfc_close(pnd); nfc_exit(context); exit(EXIT_FAILURE); } // Let the reader only try once to find a tag if (nfc_device_set_property_bool(pnd, NP_INFINITE_SELECT, false) < 0) { nfc_perror(pnd, "nfc_device_set_property_bool"); nfc_close(pnd); nfc_exit(context); exit(EXIT_FAILURE); } // Read the SAM's info const nfc_modulation nmSAM = { .nmt = NMT_ISO14443A, .nbr = NBR_106, }; nfc_target nt; int res; if ((res = nfc_initiator_select_passive_target(pnd, nmSAM, NULL, 0, &nt)) < 0) { nfc_perror(pnd, "nfc_initiator_select_passive_target"); nfc_close(pnd); nfc_exit(context); exit(EXIT_FAILURE); } else if (res == 0) { ERR("No SAM found."); nfc_close(pnd); nfc_exit(context); exit(EXIT_FAILURE); } else if (res == 1) { printf("The following ISO14443A tag (SAM) was found:\n"); print_nfc_target(&nt, true); } else { ERR("%s", "More than one ISO14442 tag found as SAM."); nfc_close(pnd); nfc_exit(context); exit(EXIT_FAILURE); } } break; case PSM_DUAL_CARD: { // FIXME Its a private pn53x function if (pn532_SAMConfiguration(pnd, mode, 0) < 0) { nfc_perror(pnd, "pn53x_SAMConfiguration"); nfc_close(pnd); nfc_exit(context); exit(EXIT_FAILURE); } uint8_t abtRx[MAX_FRAME_LEN]; nfc_target nt = { .nm = { .nmt = NMT_ISO14443A, .nbr = NBR_UNDEFINED, }, .nti = { .nai = { .abtAtqa = { 0x04, 0x00 }, .abtUid = { 0x08, 0xad, 0xbe, 0xef }, .btSak = 0x20, .szUidLen = 4, .szAtsLen = 0, }, }, };
/* * Get a list of the MIFARE targets near to the provided NFC initiator. * * The list has to be freed using the freefare_free_tags() function. */ MifareTag * freefare_get_tags (nfc_device_t *device) { MifareTag *tags = NULL; int tag_count = 0; nfc_initiator_init(device); // Drop the field for a while nfc_configure(device,NDO_ACTIVATE_FIELD,false); // Let the reader only try once to find a tag nfc_configure(device,NDO_INFINITE_SELECT,false); // Configure the CRC and Parity settings nfc_configure(device,NDO_HANDLE_CRC,true); nfc_configure(device,NDO_HANDLE_PARITY,true); // Enable field so more power consuming cards can power themselves up nfc_configure(device,NDO_ACTIVATE_FIELD,true); // Poll for a ISO14443A (MIFARE) tag nfc_target_info_t target_info; tags = malloc(sizeof (void *)); if(!tags) return NULL; tags[0] = NULL; while (nfc_initiator_select_passive_target(device,NM_ISO14443A_106,NULL,0,&target_info)) { bool found = false; struct supported_tag *tag_info; for (size_t i = 0; i < sizeof (supported_tags) / sizeof (struct supported_tag); i++) { if (((target_info.nai.szUidLen == 4) || (target_info.nai.abtUid[0] == NXP_MANUFACTURER_CODE)) && (target_info.nai.btSak == supported_tags[i].SAK) && (target_info.nai.szAtsLen == supported_tags[i].ATS_length) && (0 == memcmp (target_info.nai.abtAts, supported_tags[i].ATS, supported_tags[i].ATS_length))) { tag_info = &(supported_tags[i]); found = true; break; } } if (!found) goto deselect; tag_count++; /* (Re)Allocate memory for the found MIFARE targets array */ MifareTag *p = realloc (tags, (tag_count + 1) * sizeof (MifareTag)); if (p) tags = p; else return tags; // FAIL! Return what has been found so far. /* Allocate memory for the found MIFARE target */ switch (tag_info->type) { case CLASSIC_1K: case CLASSIC_4K: tags[tag_count-1] = mifare_classic_tag_new (); break; case DESFIRE: tags[tag_count-1] = mifare_desfire_tag_new (0); //JIM break; case ULTRALIGHT: tags[tag_count-1] = mifare_ultralight_tag_new (); break; } if (!tags[tag_count-1]) return tags; // FAIL! Return what has been found before. /* * Initialize common fields * (Target specific fields are initialized in mifare_*_tag_new()) */ (tags[tag_count-1])->device = device; (tags[tag_count-1])->info = target_info.nai; (tags[tag_count-1])->active = 0; (tags[tag_count-1])->tag_info = tag_info; tags[tag_count] = NULL; deselect: nfc_initiator_deselect_target (device); } return tags; }