Added support for NTAG 21x tags (#53)

This commit is contained in:
Martin Dagarin 2017-06-27 10:50:50 +02:00 committed by Romain Tartière
parent 2be45f60e2
commit b2eca838c4
14 changed files with 1420 additions and 1 deletions

View file

@ -12,6 +12,10 @@ set(EXAMPLES-SOURCES
mifare-desfire-ev1-configure-default-key
mifare-desfire-ev1-configure-random-uid
mifare-ultralight-info
ntag-detect
ntag-removeauth
ntag-setauth
ntag-write
)
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/../libfreefare)

View file

@ -15,7 +15,11 @@ bin_PROGRAMS = felica-lite-dump \
mifare-desfire-info \
mifare-desfire-read-ndef \
mifare-desfire-write-ndef \
mifare-ultralight-info
mifare-ultralight-info \
ntag-detect \
ntag-removeauth \
ntag-setauth \
ntag-write
felica_lite_dump_SOURCES = felica-lite-dump.c
felica_lite_dump_LDADD = $(top_builddir)/libfreefare/libfreefare.la
@ -62,4 +66,16 @@ mifare_desfire_write_ndef_LDADD = $(top_builddir)/libfreefare/libfreefare.la
mifare_ultralight_info_SOURCES = mifare-ultralight-info.c
mifare_ultralight_info_LDADD = $(top_builddir)/libfreefare/libfreefare.la
ntag_detect_SOURCES = ntag-detect.c
ntag_detect_LDADD = $(top_builddir)/libfreefare/libfreefare.la
ntag_removeauth_SOURCES = ntag-removeauth.c
ntag_removeauth_LDADD = $(top_builddir)/libfreefare/libfreefare.la
ntag_setauth_SOURCES = ntag-setauth.c
ntag_setauth_LDADD = $(top_builddir)/libfreefare/libfreefare.la
ntag_write_SOURCES = ntag-write.c
ntag_write_LDADD = $(top_builddir)/libfreefare/libfreefare.la
CLEANFILES= *.gcno

68
examples/ntag-detect.c Normal file
View file

@ -0,0 +1,68 @@
/*-
* Copyright (C) 2012, 2017 Romain Tartiere, Martin Dagarin (SloCompTech).
*
* This program is free software: you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by the
* Free Software Foundation, either version 3 of the License, or (at your
* option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>
*/
#include <err.h>
#include <stdlib.h>
#include <nfc/nfc.h>
#include <freefare.h>
int
main(int argc, char *argv[])
{
int error = EXIT_SUCCESS;
nfc_device *device = NULL;
FreefareTag *tags = NULL;
if (argc > 1)
errx(EXIT_FAILURE, "usage: %s", argv[0]);
nfc_connstring devices[8];
size_t device_count;
nfc_context *context;
nfc_init(&context);
if (context == NULL)
errx(EXIT_FAILURE, "Unable to init libnfc (malloc)");
device_count = nfc_list_devices (context, devices, sizeof (devices) / sizeof (*devices));
if (device_count <= 0)
errx(EXIT_FAILURE, "No NFC device found");
for (size_t d = 0; d < device_count; d++) {
if (!(device = nfc_open(context, devices[d]))) {
warnx("nfc_open() failed.");
error = EXIT_FAILURE;
continue;
}
if (!(tags = freefare_get_tags(device))) {
nfc_close(device);
errx(EXIT_FAILURE, "Error listing tags.");
}
for (int i = 0; (!error) && tags[i]; i++) {
char *tag_uid = freefare_get_tag_uid(tags[i]);
printf("Tag with UID %s is a %s\n", tag_uid, freefare_get_tag_friendly_name(tags[i]));
free(tag_uid);
}
freefare_free_tags(tags);
nfc_close(device);
}
nfc_exit(context);
exit(error);
}

146
examples/ntag-removeauth.c Normal file
View file

@ -0,0 +1,146 @@
/*-
* Copyright (C) 2012, 2017 Romain Tartiere, Martin Dagarin (SloCompTech).
*
* This program is free software: you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by the
* Free Software Foundation, either version 3 of the License, or (at your
* option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>
*/
#include <err.h>
#include <stdlib.h>
#include <nfc/nfc.h>
#include <freefare.h>
int
main(int argc, char *argv[])
{
int error = EXIT_SUCCESS;
nfc_device *device = NULL;
FreefareTag *tags = NULL;
if (argc > 1)
errx(EXIT_FAILURE, "usage: %s", argv[0]);
nfc_connstring devices[8];
size_t device_count;
nfc_context *context;
nfc_init(&context);
if (context == NULL)
errx(EXIT_FAILURE, "Unable to init libnfc (malloc)");
device_count = nfc_list_devices(context, devices, sizeof (devices) / sizeof (*devices));
if (device_count <= 0)
errx(EXIT_FAILURE, "No NFC device found");
for (size_t d = 0; d < device_count; d++) {
if (!(device = nfc_open(context, devices[d]))) {
warnx("nfc_open() failed.");
error = EXIT_FAILURE;
continue;
}
if (!(tags = freefare_get_tags(device))) {
nfc_close(device);
errx(EXIT_FAILURE, "Error listing tags.");
}
for (int i = 0; (!error) && tags[i]; i++) {
switch (freefare_get_tag_type(tags[i])) {
case NTAG_21x:
break;
default:
continue;
}
char *tag_uid = freefare_get_tag_uid(tags[i]);
printf("Tag with UID %s is a %s\n", tag_uid, freefare_get_tag_friendly_name(tags[i]));
FreefareTag tag = tags[i];
int res;
if (ntag21x_connect(tag) < 0)
errx(EXIT_FAILURE, "Error connecting to tag.");
uint8_t pwd[4] = {0xff,0xff,0xff,0xff};
uint8_t pack[2] = {0xaa,0xaa};
uint8_t pack_old[2] = {0x00,0x00};
NTAG21xKey key;
NTAG21xKey key_old;
key = ntag21x_key_new(pwd,pack); // Creating key
key_old = ntag21x_key_new(pwd,pack_old); // Creating key
uint8_t auth0 = 0x00; // Buffer for auth0 byte
switch (true) {
case true:
/*
Get information about tag
MUST do, because here we are recognizing tag subtype (NTAG213,NTAG215,NTAG216), and gathering all parameters
*/
res = ntag21x_get_info(tag);
if(res < 0) {
printf("Error getting info from tag\n");
break;
}
// Authenticate with tag
res = ntag21x_authenticate(tag,key);
if(res < 0) {
printf("Error getting info from tag\n");
break;
}
// Get auth byte from tag
res = ntag21x_get_auth(tag,&auth0);
if(res < 0) {
printf("Error getting auth0 byte from tag\n");
break;
}
printf("Old auth0: %#02x\n",auth0);
// Set old key
res = ntag21x_set_key(tag,key_old);
if(res < 0) {
printf("Error setting key tag\n");
break;
}
// Disable password protection (when auth0 byte > last page)
res = ntag21x_set_auth(tag,0xff);
if(res<0) {
printf("Error setting auth0 byte \n");
break;
}
// Disable read & write pwd protection -> (default: write only protection)
res = ntag21x_access_disable(tag,NTAG_PROT);
if(res < 0) {
printf("Error setting access byte \n");
break;
}
// Get auth byte from tag
res = ntag21x_get_auth(tag,&auth0);
if(res < 0) {
printf("Error getting auth0 byte from tag\n");
break;
}
printf("New auth0: %#02x\n",auth0);
}
ntag21x_disconnect(tag);
ntag21x_key_free(key); // Delete key
ntag21x_key_free(key_old); // Delete key
free(tag_uid);
}
freefare_free_tags(tags);
nfc_close(device);
}
nfc_exit(context);
exit(error);
}

146
examples/ntag-setauth.c Normal file
View file

@ -0,0 +1,146 @@
/*-
* Copyright (C) 2012, 2017 Romain Tartiere, Martin Dagarin (SloCompTech).
*
* This program is free software: you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by the
* Free Software Foundation, either version 3 of the License, or (at your
* option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>
*/
#include <err.h>
#include <stdlib.h>
#include <nfc/nfc.h>
#include <freefare.h>
int
main(int argc, char *argv[])
{
int error = EXIT_SUCCESS;
nfc_device *device = NULL;
FreefareTag *tags = NULL;
if (argc > 1)
errx(EXIT_FAILURE, "usage: %s", argv[0]);
nfc_connstring devices[8];
size_t device_count;
nfc_context *context;
nfc_init (&context);
if (context == NULL)
errx(EXIT_FAILURE, "Unable to init libnfc (malloc)");
device_count = nfc_list_devices (context, devices, sizeof (devices) / sizeof (*devices));
if (device_count <= 0)
errx(EXIT_FAILURE, "No NFC device found");
for (size_t d = 0; d < device_count; d++) {
if (!(device = nfc_open (context, devices[d]))) {
warnx("nfc_open() failed.");
error = EXIT_FAILURE;
continue;
}
if (!(tags = freefare_get_tags (device))) {
nfc_close(device);
errx(EXIT_FAILURE, "Error listing tags.");
}
for (int i = 0; (!error) && tags[i]; i++) {
switch (freefare_get_tag_type(tags[i])) {
case NTAG_21x:
break;
default:
continue;
}
char *tag_uid = freefare_get_tag_uid(tags[i]);
printf("Tag with UID %s is a %s\n", tag_uid, freefare_get_tag_friendly_name(tags[i]));
FreefareTag tag = tags[i];
int res;
if (ntag21x_connect (tag) < 0)
errx(EXIT_FAILURE, "Error connecting to tag.");
uint8_t pwd[4] = {0xff,0xff,0xff,0xff};
uint8_t pack[2] = {0xaa,0xaa};
NTAG21xKey key;
key = ntag21x_key_new(pwd,pack); // Creating key
uint8_t auth0 = 0x00; // Buffer for auth0 byte
uint8_t authlim = 0x00;
switch (true) {
case true:
/*
Get information about tag
MUST do, because here we are recognizing tag subtype (NTAG213,NTAG215,NTAG216), and gathering all parameters
*/
res = ntag21x_get_info(tag);
if(res < 0) {
printf("Error getting info from tag\n");
break;
}
// Get auth byte from tag
res = ntag21x_get_auth(tag,&auth0);
if(res < 0) {
printf("Error getting auth0 byte from tag\n");
break;
}
printf("Old auth0: %#02x\n",auth0);
res = ntag21x_get_authentication_limit(tag,&authlim);
if(res < 0) {
printf("Error getting auth0 byte from tag\n");
break;
}
printf("Authlim: %#02x\n",authlim);
// Check if auth is required to set pwd and pack
if(auth0 < ntag21x_get_last_page(tag) - 2) { // Check if last 2 pages are protected
printf("Error: pwd and PACK sections are protected with unknown password\n");
break;
}
// Set key
res = ntag21x_set_key(tag,key);
if(res < 0) {
printf("Error setting key tag\n");
break;
}
// Protect last 6 pages !! It can be hacked if you don't protect last 4 pages where auth0 byte is located
res = ntag21x_set_auth(tag,ntag21x_get_last_page(tag)-5);
if(res < 0) {
printf("Error setting auth0 byte \n");
break;
}
// Enable read & write pwd protection (default: write only protection)
res = ntag21x_access_enable(tag,NTAG_PROT);
if(res < 0) {
printf("Error setting access byte \n");
break;
}
// Get auth byte from tag
res = ntag21x_get_auth(tag,&auth0);
if(res < 0) {
printf("Error getting auth0 byte from tag\n");
break;
}
printf("New auth0: %#02x\n",auth0);
}
ntag21x_disconnect (tag);
ntag21x_key_free(key); // Delete key
free (tag_uid);
}
freefare_free_tags(tags);
nfc_close(device);
}
nfc_exit(context);
exit(error);
}

119
examples/ntag-write.c Normal file
View file

@ -0,0 +1,119 @@
/*-
* Copyright (C) 2012, 2017 Romain Tartiere, Martin Dagarin (SloCompTech).
*
* This program is free software: you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by the
* Free Software Foundation, either version 3 of the License, or (at your
* option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>
*/
#include <err.h>
#include <stdlib.h>
#include <nfc/nfc.h>
#include <freefare.h>
int
main(int argc, char *argv[])
{
int error = EXIT_SUCCESS;
nfc_device *device = NULL;
FreefareTag *tags = NULL;
if (argc > 1)
errx(EXIT_FAILURE, "usage: %s", argv[0]);
nfc_connstring devices[8];
size_t device_count;
nfc_context *context;
nfc_init(&context);
if (context == NULL)
errx(EXIT_FAILURE, "Unable to init libnfc (malloc)");
device_count = nfc_list_devices(context, devices, sizeof (devices) / sizeof (*devices));
if (device_count <= 0)
errx(EXIT_FAILURE, "No NFC device found");
for (size_t d = 0; d < device_count; d++) {
if (!(device = nfc_open(context, devices[d]))) {
warnx("nfc_open() failed.");
error = EXIT_FAILURE;
continue;
}
if (!(tags = freefare_get_tags(device))) {
nfc_close(device);
errx(EXIT_FAILURE, "Error listing tags.");
}
for (int i = 0; (!error) && tags[i]; i++) {
switch (freefare_get_tag_type(tags[i])) {
case NTAG_21x:
break;
default:
continue;
}
char *tag_uid = freefare_get_tag_uid(tags[i]);
printf("Tag with UID %s is a %s\n", tag_uid, freefare_get_tag_friendly_name (tags[i]));
FreefareTag tag = tags[i];
int res;
if (ntag21x_connect(tag) < 0)
errx(EXIT_FAILURE, "Error connecting to tag.");
uint8_t data [4] = {0xfa,0xca,0xac,0xad}; // Data to write on tag
uint8_t read[4]; // Buffer for reading data from tag
bool flag_match = true;
switch (true) {
case true:
/*
Get information about tag
MUST do, because here we are recognizing tag subtype (NTAG213,NTAG215,NTAG216), and gathering all parameters
*/
res = ntag21x_get_info(tag);
if(res < 0) {
printf("Error getting info from tag\n");
break;
}
// writing to tag 4 bytes on page 0x27 (check specs for NTAG21x before changing page number !!!)
res = ntag21x_write(tag,0x27,data);
if(res < 0) {
printf("Error writing to tag\n");
break;
}
res = ntag21x_fast_read4(tag,0x27,read); // Reading page from tag (4 bytes), you can also use ntag21x_read4 or ntag21x_read (16 bytes) or ntag21x_fast_read (start_page to end_page)
if(res < 0) {
printf("Error reading tag\n");
break;
}
for(int i=0;i < 4;i++) // Checking if we can read what we have written earlyer
if(data[i] != read[i]) {
flag_match = false;
break;
}
if(!flag_match)
printf("Data don't match\n");
else
printf("Data match\n");
}
ntag21x_disconnect(tag);
free(tag_uid);
}
freefare_free_tags(tags);
nfc_close(device);
}
nfc_exit(context);
exit(error);
}