Indent whole code using make indent. (Fixes issue 84).

This commit is contained in:
Romuald Conty 2010-09-07 17:51:03 +00:00
parent f93b4939f4
commit 18cc86a613
42 changed files with 2613 additions and 2479 deletions

View file

@ -1,59 +1,61 @@
#ifdef HAVE_CONFIG_H
#include "config.h"
# include "config.h"
#endif // HAVE_CONFIG_H
#include <stdlib.h>
#include <nfc/nfc.h>
#include <nfc/nfc-messages.h>
int main(int argc, const char* argv[])
int
main (int argc, const char *argv[])
{
nfc_device_t* pnd;
nfc_device_t *pnd;
nfc_target_info_t nti;
// Display libnfc version
const char* acLibnfcVersion = nfc_version();
printf("%s use libnfc %s\n", argv[0], acLibnfcVersion);
const char *acLibnfcVersion = nfc_version ();
printf ("%s use libnfc %s\n", argv[0], acLibnfcVersion);
// Connect using the first available NFC device
pnd = nfc_connect(NULL);
pnd = nfc_connect (NULL);
if (pnd == NULL) {
ERR("%s", "Unable to connect to NFC device.");
return EXIT_FAILURE;
ERR ("%s", "Unable to connect to NFC device.");
return EXIT_FAILURE;
}
// Set connected NFC device to initiator mode
nfc_initiator_init(pnd);
nfc_initiator_init (pnd);
// Drop the field for a while
nfc_configure(pnd,NDO_ACTIVATE_FIELD,false);
nfc_configure (pnd, NDO_ACTIVATE_FIELD, false);
// Let the reader only try once to find a tag
nfc_configure(pnd,NDO_INFINITE_SELECT,false);
nfc_configure (pnd, NDO_INFINITE_SELECT, false);
// Configure the CRC and Parity settings
nfc_configure(pnd,NDO_HANDLE_CRC,true);
nfc_configure(pnd,NDO_HANDLE_PARITY,true);
nfc_configure (pnd, NDO_HANDLE_CRC, true);
nfc_configure (pnd, NDO_HANDLE_PARITY, true);
// Enable field so more power consuming cards can power themselves up
nfc_configure(pnd,NDO_ACTIVATE_FIELD,true);
nfc_configure (pnd, NDO_ACTIVATE_FIELD, true);
printf("Connected to NFC reader: %s\n",pnd->acName);
printf ("Connected to NFC reader: %s\n", pnd->acName);
// Poll for a ISO14443A (MIFARE) tag
if (nfc_initiator_select_passive_target(pnd,NM_ISO14443A_106,NULL,0,&nti)) {
printf("The following (NFC) ISO14443A tag was found:\n");
printf(" ATQA (SENS_RES): "); print_hex(nti.nai.abtAtqa,2);
printf(" UID (NFCID%c): ",(nti.nai.abtUid[0]==0x08?'3':'1')); print_hex(nti.nai.abtUid,nti.nai.szUidLen);
printf(" SAK (SEL_RES): "); print_hex(&nti.nai.btSak,1);
if (nti.nai.szAtsLen) {
printf(" ATS (ATR): ");
print_hex(nti.nai.abtAts,nti.nai.szAtsLen);
}
if (nfc_initiator_select_passive_target (pnd, NM_ISO14443A_106, NULL, 0, &nti)) {
printf ("The following (NFC) ISO14443A tag was found:\n");
printf (" ATQA (SENS_RES): ");
print_hex (nti.nai.abtAtqa, 2);
printf (" UID (NFCID%c): ", (nti.nai.abtUid[0] == 0x08 ? '3' : '1'));
print_hex (nti.nai.abtUid, nti.nai.szUidLen);
printf (" SAK (SEL_RES): ");
print_hex (&nti.nai.btSak, 1);
if (nti.nai.szAtsLen) {
printf (" ATS (ATR): ");
print_hex (nti.nai.abtAts, nti.nai.szAtsLen);
}
}
// Disconnect from NFC device
nfc_disconnect(pnd);
nfc_disconnect (pnd);
return EXIT_SUCCESS;
}

View file

@ -17,70 +17,70 @@
* After a successful authentication it will be possible to execute other commands (e.g. Read/Write).
* The MIFARE Classic Specification (http://www.nxp.com/acrobat/other/identification/M001053_MF1ICS50_rev5_3.pdf) explains more about this process.
*/
bool nfc_initiator_mifare_cmd(nfc_device_t* pnd, const mifare_cmd mc, const uint8_t ui8Block, mifare_param* pmp)
bool
nfc_initiator_mifare_cmd (nfc_device_t * pnd, const mifare_cmd mc, const uint8_t ui8Block, mifare_param * pmp)
{
byte_t abtRx[265];
size_t szRxLen;
size_t szParamLen;
byte_t abtCmd[265];
byte_t abtRx[265];
size_t szRxLen;
size_t szParamLen;
byte_t abtCmd[265];
// Make sure we are dealing with a active device
if (!pnd->bActive) return false;
if (!pnd->bActive)
return false;
abtCmd[0] = mc; // The MIFARE Classic command
abtCmd[1] = ui8Block; // The block address (1K=0x00..0x39, 4K=0x00..0xff)
abtCmd[0] = mc; // The MIFARE Classic command
abtCmd[1] = ui8Block; // The block address (1K=0x00..0x39, 4K=0x00..0xff)
switch (mc)
{
switch (mc) {
// Read and store command have no parameter
case MC_READ:
case MC_STORE:
szParamLen = 0;
case MC_READ:
case MC_STORE:
szParamLen = 0;
break;
// Authenticate command
case MC_AUTH_A:
case MC_AUTH_B:
szParamLen = sizeof(mifare_param_auth);
case MC_AUTH_A:
case MC_AUTH_B:
szParamLen = sizeof (mifare_param_auth);
break;
// Data command
case MC_WRITE:
szParamLen = sizeof(mifare_param_data);
case MC_WRITE:
szParamLen = sizeof (mifare_param_data);
break;
// Value command
case MC_DECREMENT:
case MC_INCREMENT:
case MC_TRANSFER:
szParamLen = sizeof(mifare_param_value);
case MC_DECREMENT:
case MC_INCREMENT:
case MC_TRANSFER:
szParamLen = sizeof (mifare_param_value);
break;
// Please fix your code, you never should reach this statement
default:
return false;
default:
return false;
break;
}
// When available, copy the parameter bytes
if (szParamLen) memcpy(abtCmd+2,(byte_t*)pmp,szParamLen);
if (szParamLen)
memcpy (abtCmd + 2, (byte_t *) pmp, szParamLen);
// Fire the mifare command
if (!nfc_initiator_transceive_bytes(pnd,abtCmd,2+szParamLen,abtRx,&szRxLen)) {
if (!nfc_initiator_transceive_bytes (pnd, abtCmd, 2 + szParamLen, abtRx, &szRxLen)) {
if (pnd->iLastError != 0x14)
nfc_perror (pnd, "nfc_initiator_transceive_bytes");
nfc_perror (pnd, "nfc_initiator_transceive_bytes");
return false;
}
// When we have executed a read command, copy the received bytes into the param
if (mc == MC_READ) {
if(szRxLen == 16) {
memcpy(pmp->mpd.abtData,abtRx,16);
if (szRxLen == 16) {
memcpy (pmp->mpd.abtData, abtRx, 16);
} else {
return false;
}
}
// Command succesfully executed
return true;
}

View file

@ -22,36 +22,36 @@
*/
#ifndef _LIBNFC_MIFARE_H_
#define _LIBNFC_MIFARE_H_
# define _LIBNFC_MIFARE_H_
#include <nfc/nfc-types.h>
# include <nfc/nfc-types.h>
// Compiler directive, set struct alignment to 1 byte_t for compatibility
#pragma pack(1)
# pragma pack(1)
typedef enum {
MC_AUTH_A = 0x60,
MC_AUTH_B = 0x61,
MC_READ = 0x30,
MC_WRITE = 0xA0,
MC_TRANSFER = 0xB0,
MC_DECREMENT = 0xC0,
MC_INCREMENT = 0xC1,
MC_STORE = 0xC2
MC_AUTH_A = 0x60,
MC_AUTH_B = 0x61,
MC_READ = 0x30,
MC_WRITE = 0xA0,
MC_TRANSFER = 0xB0,
MC_DECREMENT = 0xC0,
MC_INCREMENT = 0xC1,
MC_STORE = 0xC2
} mifare_cmd;
// MIFARE command params
typedef struct {
byte_t abtKey[6];
byte_t abtUid[4];
byte_t abtKey[6];
byte_t abtUid[4];
} mifare_param_auth;
typedef struct {
byte_t abtData[16];
byte_t abtData[16];
} mifare_param_data;
typedef struct {
byte_t abtValue[4];
byte_t abtValue[4];
} mifare_param_value;
typedef union {
@ -61,30 +61,30 @@ typedef union {
} mifare_param;
// Reset struct alignment to default
#pragma pack()
# pragma pack()
bool nfc_initiator_mifare_cmd(nfc_device_t* pnd, const mifare_cmd mc, const uint8_t ui8Block, mifare_param* pmp);
bool nfc_initiator_mifare_cmd (nfc_device_t * pnd, const mifare_cmd mc, const uint8_t ui8Block, mifare_param * pmp);
// Compiler directive, set struct alignment to 1 byte_t for compatibility
#pragma pack(1)
# pragma pack(1)
// MIFARE Classic
typedef struct {
byte_t abtUID[4];
byte_t btBCC;
byte_t btUnknown;
byte_t abtATQA[2];
byte_t abtUnknown[8];
byte_t abtUID[4];
byte_t btBCC;
byte_t btUnknown;
byte_t abtATQA[2];
byte_t abtUnknown[8];
} mifare_classic_block_manufacturer;
typedef struct {
byte_t abtData[16];
byte_t abtData[16];
} mifare_classic_block_data;
typedef struct {
byte_t abtKeyA[6];
byte_t abtAccessBits[4];
byte_t abtKeyB[6];
byte_t abtKeyA[6];
byte_t abtAccessBits[4];
byte_t abtKeyB[6];
} mifare_classic_block_trailer;
typedef union {
@ -99,17 +99,17 @@ typedef struct {
// MIFARE Ultralight
typedef struct {
byte_t sn0[3];
byte_t btBCC0;
byte_t sn1[4];
byte_t btBCC1;
byte_t internal;
byte_t lock[2];
byte_t otp[4];
byte_t sn0[3];
byte_t btBCC0;
byte_t sn1[4];
byte_t btBCC1;
byte_t internal;
byte_t lock[2];
byte_t otp[4];
} mifareul_block_manufacturer;
typedef struct {
byte_t abtData[16];
byte_t abtData[16];
} mifareul_block_data;
typedef union {
@ -122,6 +122,6 @@ typedef struct {
} mifareul_tag;
// Reset struct alignment to default
#pragma pack()
# pragma pack()
#endif // _LIBNFC_MIFARE_H_

View file

@ -23,7 +23,7 @@
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
# include "config.h"
#endif // HAVE_CONFIG_H
#include <stdio.h>
@ -47,152 +47,143 @@ static size_t szRxBits;
static size_t szRxLen;
static byte_t abtUid[10];
static size_t szUidLen = 4;
static nfc_device_t* pnd;
static nfc_device_t *pnd;
bool quiet_output = false;
bool quiet_output = false;
// ISO14443A Anti-Collision Commands
byte_t abtReqa [1] = { 0x26 };
byte_t abtSelectAll [2] = { 0x93,0x20 };
byte_t abtSelectTag [9] = { 0x93,0x70,0x00,0x00,0x00,0x00,0x00,0x00,0x00 };
byte_t abtRats [4] = { 0xe0,0x50,0xbc,0xa5 };
byte_t abtHalt [4] = { 0x50,0x00,0x57,0xcd };
byte_t abtReqa[1] = { 0x26 };
byte_t abtSelectAll[2] = { 0x93, 0x20 };
byte_t abtSelectTag[9] = { 0x93, 0x70, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
byte_t abtRats[4] = { 0xe0, 0x50, 0xbc, 0xa5 };
byte_t abtHalt[4] = { 0x50, 0x00, 0x57, 0xcd };
static bool transmit_bits(const byte_t* pbtTx, const size_t szTxBits)
static bool
transmit_bits (const byte_t * pbtTx, const size_t szTxBits)
{
// Show transmitted command
if(!quiet_output)
{
printf("R: ");
print_hex_bits(pbtTx,szTxBits);
if (!quiet_output) {
printf ("R: ");
print_hex_bits (pbtTx, szTxBits);
}
// Transmit the bit frame command, we don't use the arbitrary parity feature
if (!nfc_initiator_transceive_bits(pnd,pbtTx,szTxBits,NULL,abtRx,&szRxBits,NULL)) return false;
if (!nfc_initiator_transceive_bits (pnd, pbtTx, szTxBits, NULL, abtRx, &szRxBits, NULL))
return false;
// Show received answer
if(!quiet_output)
{
printf("T: ");
print_hex_bits(abtRx,szRxBits);
if (!quiet_output) {
printf ("T: ");
print_hex_bits (abtRx, szRxBits);
}
// Succesful transfer
return true;
}
static bool transmit_bytes(const byte_t* pbtTx, const size_t szTxLen)
static bool
transmit_bytes (const byte_t * pbtTx, const size_t szTxLen)
{
// Show transmitted command
if(!quiet_output)
{
printf("R: ");
print_hex(pbtTx,szTxLen);
if (!quiet_output) {
printf ("R: ");
print_hex (pbtTx, szTxLen);
}
// Transmit the command bytes
if (!nfc_initiator_transceive_bytes(pnd,pbtTx,szTxLen,abtRx,&szRxLen)) return false;
if (!nfc_initiator_transceive_bytes (pnd, pbtTx, szTxLen, abtRx, &szRxLen))
return false;
// Show received answer
if(!quiet_output)
{
printf("T: ");
print_hex(abtRx,szRxLen);
if (!quiet_output) {
printf ("T: ");
print_hex (abtRx, szRxLen);
}
// Succesful transfer
return true;
}
static void print_usage(char* argv[])
static void
print_usage (char *argv[])
{
printf("Usage: %s [OPTIONS]\n", argv[0]);
printf("Options:\n");
printf("\t-h\tHelp. Print this message.\n");
printf("\t-q\tQuiet mode. Suppress output of READER and EMULATOR data (improves timing).\n");
printf ("Usage: %s [OPTIONS]\n", argv[0]);
printf ("Options:\n");
printf ("\t-h\tHelp. Print this message.\n");
printf ("\t-q\tQuiet mode. Suppress output of READER and EMULATOR data (improves timing).\n");
}
int main(int argc,char* argv[])
int
main (int argc, char *argv[])
{
int arg;
int arg;
// Get commandline options
for (arg=1;arg<argc;arg++) {
if (0 == strcmp(argv[arg], "-h")) {
print_usage(argv);
for (arg = 1; arg < argc; arg++) {
if (0 == strcmp (argv[arg], "-h")) {
print_usage (argv);
return 0;
} else if (0 == strcmp(argv[arg], "-q")) {
INFO("%s", "Quiet mode.");
} else if (0 == strcmp (argv[arg], "-q")) {
INFO ("%s", "Quiet mode.");
quiet_output = true;
} else {
ERR("%s is not supported option.", argv[arg]);
print_usage(argv);
ERR ("%s is not supported option.", argv[arg]);
print_usage (argv);
return -1;
}
}
// Try to open the NFC reader
pnd = nfc_connect(NULL);
pnd = nfc_connect (NULL);
if (!pnd)
{
printf("Error connecting NFC reader\n");
if (!pnd) {
printf ("Error connecting NFC reader\n");
return 1;
}
nfc_initiator_init(pnd);
nfc_initiator_init (pnd);
// Drop the field for a while
if (!nfc_configure(pnd,NDO_ACTIVATE_FIELD,false)) {
if (!nfc_configure (pnd, NDO_ACTIVATE_FIELD, false)) {
nfc_perror (pnd, "nfc_configure");
exit (EXIT_FAILURE);
}
// Configure the CRC
if (!nfc_configure(pnd,NDO_HANDLE_CRC,false)) {
if (!nfc_configure (pnd, NDO_HANDLE_CRC, false)) {
nfc_perror (pnd, "nfc_configure");
exit (EXIT_FAILURE);
}
// Configure parity settings
if (!nfc_configure(pnd,NDO_HANDLE_PARITY,true)) {
if (!nfc_configure (pnd, NDO_HANDLE_PARITY, true)) {
nfc_perror (pnd, "nfc_configure");
exit (EXIT_FAILURE);
}
// Enable field so more power consuming cards can power themselves up
if (!nfc_configure(pnd,NDO_ACTIVATE_FIELD,true)) {
if (!nfc_configure (pnd, NDO_ACTIVATE_FIELD, true)) {
nfc_perror (pnd, "nfc_configure");
exit (EXIT_FAILURE);
}
if (!nfc_configure(pnd, NDO_EASY_FRAMING, false)) {
nfc_perror (pnd, "nfc_configure");
exit (EXIT_FAILURE);
if (!nfc_configure (pnd, NDO_EASY_FRAMING, false)) {
nfc_perror (pnd, "nfc_configure");
exit (EXIT_FAILURE);
}
printf("\nConnected to NFC reader: %s\n\n",pnd->acName);
printf ("\nConnected to NFC reader: %s\n\n", pnd->acName);
// Send the 7 bits request command specified in ISO 14443A (0x26)
if (!transmit_bits(abtReqa,7))
{
printf("Error: No tag available\n");
nfc_disconnect(pnd);
if (!transmit_bits (abtReqa, 7)) {
printf ("Error: No tag available\n");
nfc_disconnect (pnd);
return 1;
}
// Anti-collision
transmit_bytes(abtSelectAll,2);
transmit_bytes (abtSelectAll, 2);
// Save the UID
memcpy(abtUid,abtRx,4);
memcpy(abtSelectTag+2,abtRx,5);
append_iso14443a_crc(abtSelectTag,7);
transmit_bytes(abtSelectTag,9);
memcpy (abtUid, abtRx, 4);
memcpy (abtSelectTag + 2, abtRx, 5);
append_iso14443a_crc (abtSelectTag, 7);
transmit_bytes (abtSelectTag, 9);
// Test if we are dealing with a 4 bytes uid
if (abtUid[0]!= 0x88)
{
if (abtUid[0] != 0x88) {
szUidLen = 4;
} else {
// We have to do the anti-collision for cascade level 2
@ -200,28 +191,29 @@ int main(int argc,char* argv[])
abtSelectTag[0] = 0x95;
// Anti-collision
transmit_bytes(abtSelectAll,2);
transmit_bytes (abtSelectAll, 2);
// Save the UID
memcpy(abtUid+4,abtRx,4);
memcpy(abtSelectTag+2,abtRx,5);
append_iso14443a_crc(abtSelectTag,7);
transmit_bytes(abtSelectTag,9);
memcpy (abtUid + 4, abtRx, 4);
memcpy (abtSelectTag + 2, abtRx, 5);
append_iso14443a_crc (abtSelectTag, 7);
transmit_bytes (abtSelectTag, 9);
szUidLen = 7;
}
// Request ATS, this only applies to tags that support ISO 14443A-4
if (abtRx[0] & SAK_FLAG_ATS_SUPPORTED) transmit_bytes(abtRats,4);
if (abtRx[0] & SAK_FLAG_ATS_SUPPORTED)
transmit_bytes (abtRats, 4);
// Done, halt the tag now
transmit_bytes(abtHalt,4);
transmit_bytes (abtHalt, 4);
printf("\nFound tag with UID: ");
printf ("\nFound tag with UID: ");
if (szUidLen == 7) {
printf("%02x%02x%02x", abtUid[6], abtUid[5], abtUid[4]);
printf ("%02x%02x%02x", abtUid[6], abtUid[5], abtUid[4]);
}
printf("%02x%02x%02x%02x\n", abtUid[3], abtUid[2], abtUid[1], abtUid[0]);
printf ("%02x%02x%02x%02x\n", abtUid[3], abtUid[2], abtUid[1], abtUid[0]);
nfc_disconnect(pnd);
nfc_disconnect (pnd);
return 0;
}

View file

@ -23,7 +23,7 @@
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
# include "config.h"
#endif // HAVE_CONFIG_H
#include <stdio.h>
@ -41,138 +41,132 @@
static byte_t abtRecv[MAX_FRAME_LEN];
static size_t szRecvBits;
static nfc_device_t* pnd;
static nfc_device_t *pnd;
// ISO14443A Anti-Collision response
byte_t abtAtqa [2] = { 0x04,0x00 };
byte_t abtUidBcc [5] = { 0xDE,0xAD,0xBE,0xAF,0x62 };
byte_t abtSak [9] = { 0x08,0xb6,0xdd };
byte_t abtAtqa[2] = { 0x04, 0x00 };
byte_t abtUidBcc[5] = { 0xDE, 0xAD, 0xBE, 0xAF, 0x62 };
byte_t abtSak[9] = { 0x08, 0xb6, 0xdd };
void print_usage(char* argv[])
void
print_usage (char *argv[])
{
printf("Usage: %s [OPTIONS] [UID]\n", argv[0]);
printf("Options:\n");
printf("\t-h\tHelp. Print this message.\n");
printf("\t-q\tQuiet mode. Suppress output of READER and EMULATOR data (improves timing).\n");
printf("\n");
printf("\t[UID]\tUID to emulate, specified as 8 HEX digits (default is DEADBEAF).\n");
printf ("Usage: %s [OPTIONS] [UID]\n", argv[0]);
printf ("Options:\n");
printf ("\t-h\tHelp. Print this message.\n");
printf ("\t-q\tQuiet mode. Suppress output of READER and EMULATOR data (improves timing).\n");
printf ("\n");
printf ("\t[UID]\tUID to emulate, specified as 8 HEX digits (default is DEADBEAF).\n");
}
int main(int argc, char *argv[])
int
main (int argc, char *argv[])
{
byte_t* pbtTx = NULL;
size_t szTxBits;
bool quiet_output = false;
byte_t *pbtTx = NULL;
size_t szTxBits;
bool quiet_output = false;
int arg, i;
int arg,
i;
// Get commandline options
for (arg=1;arg<argc;arg++) {
if (0 == strcmp(argv[arg], "-h")) {
print_usage(argv);
for (arg = 1; arg < argc; arg++) {
if (0 == strcmp (argv[arg], "-h")) {
print_usage (argv);
return 0;
} else if (0 == strcmp(argv[arg], "-q")) {
INFO("%s", "Quiet mode.");
} else if (0 == strcmp (argv[arg], "-q")) {
INFO ("%s", "Quiet mode.");
quiet_output = true;
} else if((arg == argc-1) && (strlen(argv[arg]) == 8)) { // See if UID was specified as HEX string
byte_t abtTmp[3] = { 0x00,0x00,0x00 };
printf("[+] Using UID: %s\n",argv[arg]);
abtUidBcc[4]= 0x00;
for(i= 0; i < 4; ++i)
{
memcpy(abtTmp,argv[arg]+i*2,2);
abtUidBcc[i]= (byte_t) strtol((char*)abtTmp,NULL,16);
} else if ((arg == argc - 1) && (strlen (argv[arg]) == 8)) { // See if UID was specified as HEX string
byte_t abtTmp[3] = { 0x00, 0x00, 0x00 };
printf ("[+] Using UID: %s\n", argv[arg]);
abtUidBcc[4] = 0x00;
for (i = 0; i < 4; ++i) {
memcpy (abtTmp, argv[arg] + i * 2, 2);
abtUidBcc[i] = (byte_t) strtol ((char *) abtTmp, NULL, 16);
abtUidBcc[4] ^= abtUidBcc[i];
}
} else {
ERR("%s is not supported option.", argv[arg]);
print_usage(argv);
ERR ("%s is not supported option.", argv[arg]);
print_usage (argv);
return -1;
}
}
// Try to open the NFC reader
pnd = nfc_connect(NULL);
pnd = nfc_connect (NULL);
if (pnd == NULL)
{
printf("Error connecting NFC reader\n");
if (pnd == NULL) {
printf ("Error connecting NFC reader\n");
return 1;
}
printf("\n");
printf("[+] Connected to NFC reader: %s\n",pnd->acName);
printf("[+] Try to break out the auto-emulation, this requires a second reader!\n");
printf("[+] To do this, please send any command after the anti-collision\n");
printf("[+] For example, send a RATS command or use the \"nfc-anticol\" tool\n");
if (!nfc_target_init(pnd,abtRecv,&szRecvBits))
{
printf("Error: Could not come out of auto-emulation, no command was received\n");
printf ("\n");
printf ("[+] Connected to NFC reader: %s\n", pnd->acName);
printf ("[+] Try to break out the auto-emulation, this requires a second reader!\n");
printf ("[+] To do this, please send any command after the anti-collision\n");
printf ("[+] For example, send a RATS command or use the \"nfc-anticol\" tool\n");
if (!nfc_target_init (pnd, abtRecv, &szRecvBits)) {
printf ("Error: Could not come out of auto-emulation, no command was received\n");
return 1;
}
printf("[+] Received initiator command: ");
print_hex_bits(abtRecv,szRecvBits);
printf("[+] Configuring communication\n");
if (!nfc_configure(pnd,NDO_HANDLE_CRC,false) || !nfc_configure(pnd,NDO_HANDLE_PARITY,true)) {
nfc_perror(pnd, "nfc_configure");
exit(EXIT_FAILURE);
printf ("[+] Received initiator command: ");
print_hex_bits (abtRecv, szRecvBits);
printf ("[+] Configuring communication\n");
if (!nfc_configure (pnd, NDO_HANDLE_CRC, false) || !nfc_configure (pnd, NDO_HANDLE_PARITY, true)) {
nfc_perror (pnd, "nfc_configure");
exit (EXIT_FAILURE);
}
printf("[+] Done, the emulated tag is initialized with UID: %02X%02X%02X%02X\n\n",abtUidBcc[0],abtUidBcc[1],abtUidBcc[2],abtUidBcc[3]);
printf ("[+] Done, the emulated tag is initialized with UID: %02X%02X%02X%02X\n\n", abtUidBcc[0], abtUidBcc[1],
abtUidBcc[2], abtUidBcc[3]);
while(true)
{
while (true) {
// Test if we received a frame
if (nfc_target_receive_bits(pnd,abtRecv,&szRecvBits,NULL))
{
if (nfc_target_receive_bits (pnd, abtRecv, &szRecvBits, NULL)) {
// Prepare the command to send back for the anti-collision request
switch(szRecvBits)
{
case 7: // Request or Wakeup
pbtTx = abtAtqa;
szTxBits = 16;
// New anti-collsion session started
if (!quiet_output) printf("\n");
switch (szRecvBits) {
case 7: // Request or Wakeup
pbtTx = abtAtqa;
szTxBits = 16;
// New anti-collsion session started
if (!quiet_output)
printf ("\n");
break;
case 16: // Select All
pbtTx = abtUidBcc;
szTxBits = 40;
case 16: // Select All
pbtTx = abtUidBcc;
szTxBits = 40;
break;
case 72: // Select Tag
pbtTx = abtSak;
szTxBits = 24;
case 72: // Select Tag
pbtTx = abtSak;
szTxBits = 24;
break;
default: // unknown length?
szTxBits = 0;
default: // unknown length?
szTxBits = 0;
break;
}
if(!quiet_output)
{
printf("R: ");
print_hex_bits(abtRecv,szRecvBits);
if (!quiet_output) {
printf ("R: ");
print_hex_bits (abtRecv, szRecvBits);
}
// Test if we know how to respond
if(szTxBits)
{
if (szTxBits) {
// Send and print the command to the screen
if (!nfc_target_send_bits(pnd,pbtTx,szTxBits,NULL)) {
nfc_perror(pnd, "nfc_target_send_bits");
exit(EXIT_FAILURE);
if (!nfc_target_send_bits (pnd, pbtTx, szTxBits, NULL)) {
nfc_perror (pnd, "nfc_target_send_bits");
exit (EXIT_FAILURE);
}
if(!quiet_output)
{
printf("T: ");
print_hex_bits(pbtTx,szTxBits);
if (!quiet_output) {
printf ("T: ");
print_hex_bits (pbtTx, szTxBits);
}
}
}
}
nfc_disconnect(pnd);
exit(EXIT_SUCCESS);
nfc_disconnect (pnd);
exit (EXIT_SUCCESS);
}

View file

@ -23,14 +23,14 @@
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
# include "config.h"
#endif // HAVE_CONFIG_H
#ifdef HAVE_LIBUSB
#ifdef DEBUG
#include <sys/param.h>
#include <usb.h>
#endif
# ifdef DEBUG
# include <sys/param.h>
# include <usb.h>
# endif
#endif
#include <err.h>
@ -46,35 +46,35 @@
#define MAX_DEVICE_COUNT 16
#define MAX_TARGET_COUNT 16
static nfc_device_t* pnd;
static nfc_device_t *pnd;
int main(int argc, const char* argv[])
int
main (int argc, const char *argv[])
{
const char* acLibnfcVersion;
size_t szDeviceFound;
size_t szTargetFound;
size_t i;
const char *acLibnfcVersion;
size_t szDeviceFound;
size_t szTargetFound;
size_t i;
nfc_device_desc_t *pnddDevices;
// Display libnfc version
acLibnfcVersion = nfc_version();
printf("%s use libnfc %s\n", argv[0], acLibnfcVersion);
pnddDevices = parse_device_desc(argc, argv, &szDeviceFound);
// Display libnfc version
acLibnfcVersion = nfc_version ();
printf ("%s use libnfc %s\n", argv[0], acLibnfcVersion);
pnddDevices = parse_device_desc (argc, argv, &szDeviceFound);
if (argc > 1 && szDeviceFound == 0) {
errx (1, "usage: %s [--device driver:port:speed]", argv[0]);
}
#ifdef HAVE_LIBUSB
#ifdef DEBUG
usb_set_debug(4);
#endif
#endif
#ifdef HAVE_LIBUSB
# ifdef DEBUG
usb_set_debug (4);
# endif
#endif
/* Lazy way to open an NFC device */
#if 0
pnd = nfc_connect(NULL);
pnd = nfc_connect (NULL);
#endif
/* If specific device is wanted, i.e. an ARYGON device on /dev/ttyUSB0 */
@ -84,13 +84,11 @@ int main(int argc, const char* argv[])
ndd.pcPort = "/dev/ttyUSB0";
ndd.uiSpeed = 115200;
pnd = nfc_connect(&ndd);
pnd = nfc_connect (&ndd);
#endif
if (szDeviceFound == 0)
{
if (!(pnddDevices = malloc (MAX_DEVICE_COUNT * sizeof (*pnddDevices))))
{
if (szDeviceFound == 0) {
if (!(pnddDevices = malloc (MAX_DEVICE_COUNT * sizeof (*pnddDevices)))) {
fprintf (stderr, "malloc() failed\n");
return EXIT_FAILURE;
}
@ -98,94 +96,88 @@ int main(int argc, const char* argv[])
nfc_list_devices (pnddDevices, MAX_DEVICE_COUNT, &szDeviceFound);
}
if (szDeviceFound == 0)
{
INFO("%s", "No device found.");
if (szDeviceFound == 0) {
INFO ("%s", "No device found.");
}
for (i = 0; i < szDeviceFound; i++)
{
for (i = 0; i < szDeviceFound; i++) {
nfc_target_info_t anti[MAX_TARGET_COUNT];
pnd = nfc_connect(&(pnddDevices[i]));
pnd = nfc_connect (&(pnddDevices[i]));
if (pnd == NULL)
{
ERR("%s", "Unable to connect to NFC device.");
if (pnd == NULL) {
ERR ("%s", "Unable to connect to NFC device.");
return 1;
}
nfc_initiator_init(pnd);
nfc_initiator_init (pnd);
// Drop the field for a while
if (!nfc_configure(pnd,NDO_ACTIVATE_FIELD,false)) {
nfc_perror(pnd, "nfc_configure");
exit(EXIT_FAILURE);
if (!nfc_configure (pnd, NDO_ACTIVATE_FIELD, false)) {
nfc_perror (pnd, "nfc_configure");
exit (EXIT_FAILURE);
}
// Let the reader only try once to find a tag
if (!nfc_configure(pnd,NDO_INFINITE_SELECT,false)) {
nfc_perror(pnd, "nfc_configure");
exit(EXIT_FAILURE);
if (!nfc_configure (pnd, NDO_INFINITE_SELECT, false)) {
nfc_perror (pnd, "nfc_configure");
exit (EXIT_FAILURE);
}
// Configure the CRC and Parity settings
if (!nfc_configure(pnd,NDO_HANDLE_CRC,true)) {
nfc_perror(pnd, "nfc_configure");
exit(EXIT_FAILURE);
if (!nfc_configure (pnd, NDO_HANDLE_CRC, true)) {
nfc_perror (pnd, "nfc_configure");
exit (EXIT_FAILURE);
}
if (!nfc_configure(pnd,NDO_HANDLE_PARITY,true)) {
nfc_perror(pnd, "nfc_configure");
exit(EXIT_FAILURE);
if (!nfc_configure (pnd, NDO_HANDLE_PARITY, true)) {
nfc_perror (pnd, "nfc_configure");
exit (EXIT_FAILURE);
}
// Enable field so more power consuming cards can power themselves up
if (!nfc_configure(pnd,NDO_ACTIVATE_FIELD,true)) {
nfc_perror(pnd, "nfc_configure");
exit(EXIT_FAILURE);
if (!nfc_configure (pnd, NDO_ACTIVATE_FIELD, true)) {
nfc_perror (pnd, "nfc_configure");
exit (EXIT_FAILURE);
}
if (!nfc_configure(pnd, NDO_AUTO_ISO14443_4, true)) {
nfc_perror(pnd, "nfc_configure");
exit(EXIT_FAILURE);
if (!nfc_configure (pnd, NDO_AUTO_ISO14443_4, true)) {
nfc_perror (pnd, "nfc_configure");
exit (EXIT_FAILURE);
}
printf("Connected to NFC reader: %s\n",pnd->acName);
printf ("Connected to NFC reader: %s\n", pnd->acName);
// List ISO14443A targets
if (nfc_initiator_list_passive_targets(pnd, NM_ISO14443A_106, anti, MAX_TARGET_COUNT, &szTargetFound )) {
size_t n;
printf("%d ISO14443A passive target(s) was found%s\n", (int)szTargetFound, (szTargetFound==0)?".\n":":");
for(n=0; n<szTargetFound; n++) {
if (nfc_initiator_list_passive_targets (pnd, NM_ISO14443A_106, anti, MAX_TARGET_COUNT, &szTargetFound)) {
size_t n;
printf ("%d ISO14443A passive target(s) was found%s\n", (int) szTargetFound, (szTargetFound == 0) ? ".\n" : ":");
for (n = 0; n < szTargetFound; n++) {
print_nfc_iso14443a_info (anti[n].nai);
printf("\n");
printf ("\n");
}
}
// List Felica tags
if (nfc_initiator_list_passive_targets(pnd, NM_FELICA_212, anti, MAX_TARGET_COUNT, &szTargetFound )) {
size_t n;
printf("%d Felica (212 kbps) passive target(s) was found%s\n", (int)szTargetFound, (szTargetFound==0)?".\n":":");
for(n=0; n<szTargetFound; n++) {
if (nfc_initiator_list_passive_targets (pnd, NM_FELICA_212, anti, MAX_TARGET_COUNT, &szTargetFound)) {
size_t n;
printf ("%d Felica (212 kbps) passive target(s) was found%s\n", (int) szTargetFound,
(szTargetFound == 0) ? ".\n" : ":");
for (n = 0; n < szTargetFound; n++) {
print_nfc_felica_info (anti[n].nfi);
printf("\n");
printf ("\n");
}
}
if (nfc_initiator_list_passive_targets(pnd, NM_FELICA_424, anti, MAX_TARGET_COUNT, &szTargetFound )) {
size_t n;
printf("%d Felica (424 kbps) passive target(s) was found%s\n", (int)szTargetFound, (szTargetFound==0)?".\n":":");
for(n=0; n<szTargetFound; n++) {
if (nfc_initiator_list_passive_targets (pnd, NM_FELICA_424, anti, MAX_TARGET_COUNT, &szTargetFound)) {
size_t n;
printf ("%d Felica (424 kbps) passive target(s) was found%s\n", (int) szTargetFound,
(szTargetFound == 0) ? ".\n" : ":");
for (n = 0; n < szTargetFound; n++) {
print_nfc_felica_info (anti[n].nfi);
printf("\n");
printf ("\n");
}
}
// List ISO14443B targets
if (nfc_initiator_list_passive_targets(pnd, NM_ISO14443B_106, anti, MAX_TARGET_COUNT, &szTargetFound )) {
size_t n;
printf("%d ISO14443B passive target(s) was found%s\n", (int)szTargetFound, (szTargetFound==0)?".\n":":");
for(n=0; n<szTargetFound; n++) {
if (nfc_initiator_list_passive_targets (pnd, NM_ISO14443B_106, anti, MAX_TARGET_COUNT, &szTargetFound)) {
size_t n;
printf ("%d ISO14443B passive target(s) was found%s\n", (int) szTargetFound, (szTargetFound == 0) ? ".\n" : ":");
for (n = 0; n < szTargetFound; n++) {
print_nfc_iso14443b_info (anti[n].nbi);
printf("\n");
printf ("\n");
}
}
@ -199,8 +191,8 @@ int main(int argc, const char* argv[])
}
}
*/
nfc_disconnect(pnd);
}
nfc_disconnect (pnd);
}
free (pnddDevices);
return 0;

View file

@ -69,7 +69,7 @@ print_success_or_failure (bool bFailure, uint32_t * uiBlockCounter)
*uiBlockCounter += (*uiBlockCounter < 128) ? 4 : 16;
}
static bool
static bool
is_first_block (uint32_t uiBlock)
{
// Test if we are in the small or big sectors
@ -79,7 +79,7 @@ is_first_block (uint32_t uiBlock)
return ((uiBlock) % 16 == 0);
}
static bool
static bool
is_trailer_block (uint32_t uiBlock)
{
// Test if we are in the small or big sectors
@ -89,7 +89,7 @@ is_trailer_block (uint32_t uiBlock)
return ((uiBlock + 1) % 16 == 0);
}
static uint32_t
static uint32_t
get_trailer_block (uint32_t uiFirstBlock)
{
// Test if we are in the small or big sectors
@ -102,12 +102,12 @@ get_trailer_block (uint32_t uiFirstBlock)
return trailer_block;
}
static bool
static bool
authenticate (uint32_t uiBlock)
{
mifare_cmd mc;
uint32_t uiTrailerBlock;
size_t key_index;
size_t key_index;
// Key file authentication.
if (bUseKeyFile) {
@ -156,11 +156,11 @@ authenticate (uint32_t uiBlock)
return false;
}
static bool
static bool
read_card (void)
{
int32_t iBlock;
bool bFailure = false;
bool bFailure = false;
uint32_t uiReadBlocks = 0;
printf ("Reading out %d blocks |", uiBlocks + 1);
@ -197,7 +197,7 @@ read_card (void)
memcpy (mtDump.amb[iBlock].mbt.abtAccessBits, mp.mpd.abtData + 6, 4);
memcpy (mtDump.amb[iBlock].mbt.abtKeyB, mtKeys.amb[iBlock].mbt.abtKeyB, 6);
} else {
printf("!\nError: unable to read trailer block 0x%02x\n", iBlock);
printf ("!\nError: unable to read trailer block 0x%02x\n", iBlock);
}
} else {
// Make sure a earlier readout did not fail
@ -207,7 +207,7 @@ read_card (void)
memcpy (mtDump.amb[iBlock].mbd.abtData, mp.mpd.abtData, 16);
} else {
bFailure = true;
printf("!\nError: unable to read block 0x%02x\n", iBlock);
printf ("!\nError: unable to read block 0x%02x\n", iBlock);
return false;
}
}
@ -221,11 +221,11 @@ read_card (void)
return true;
}
static bool
static bool
write_card (void)
{
uint32_t uiBlock;
bool bFailure = false;
bool bFailure = false;
uint32_t uiWriteBlocks = 0;
printf ("Writing %d blocks |", uiBlocks + 1);
@ -295,8 +295,8 @@ mifare_classic_extract_payload (const char *abDump, char *pbPayload)
{
uint8_t uiSectorIndex;
uint8_t uiBlockIndex;
size_t szDumpOffset;
size_t szPayloadIndex = 0;
size_t szDumpOffset;
size_t szPayloadIndex = 0;
for (uiSectorIndex = 1; uiSectorIndex < 16; uiSectorIndex++) {
for (uiBlockIndex = 0; uiBlockIndex < 3; uiBlockIndex++) {
@ -308,8 +308,7 @@ mifare_classic_extract_payload (const char *abDump, char *pbPayload)
}
}
typedef enum
{
typedef enum {
ACTION_READ,
ACTION_WRITE,
ACTION_EXTRACT,
@ -335,11 +334,11 @@ print_usage (const char *pcProgramName)
int
main (int argc, const char *argv[])
{
bool b4K;
bool b4K;
action_t atAction = ACTION_USAGE;
byte_t *pbtUID;
FILE *pfKeys = NULL;
FILE *pfDump = NULL;
FILE *pfKeys = NULL;
FILE *pfDump = NULL;
const char *command = argv[1];
if (argc < 2) {
@ -415,32 +414,29 @@ main (int argc, const char *argv[])
// Drop the field for a while
if (!nfc_configure (pnd, NDO_ACTIVATE_FIELD, false)) {
nfc_perror(pnd, "nfc_configure");
nfc_perror (pnd, "nfc_configure");
exit (EXIT_FAILURE);
}
// Let the reader only try once to find a tag
if (!nfc_configure (pnd, NDO_INFINITE_SELECT, false)) {
nfc_perror(pnd, "nfc_configure");
nfc_perror (pnd, "nfc_configure");
exit (EXIT_FAILURE);
}
if (!nfc_configure (pnd, NDO_HANDLE_CRC, true)) {
nfc_perror(pnd, "nfc_configure");
nfc_perror (pnd, "nfc_configure");
exit (EXIT_FAILURE);
}
if (!nfc_configure (pnd, NDO_HANDLE_PARITY, true)) {
nfc_perror(pnd, "nfc_configure");
nfc_perror (pnd, "nfc_configure");
exit (EXIT_FAILURE);
}
// Enable field so more power consuming cards can power themselves up
if (!nfc_configure (pnd, NDO_ACTIVATE_FIELD, true)) {
nfc_perror(pnd, "nfc_configure");
nfc_perror (pnd, "nfc_configure");
exit (EXIT_FAILURE);
}
// Disable ISO14443-4 switching in order to read devices that emulate Mifare Classic with ISO14443-4 compliance.
nfc_configure(pnd, NDO_AUTO_ISO14443_4, false);
nfc_configure (pnd, NDO_AUTO_ISO14443_4, false);
printf ("Connected to NFC reader: %s\n", pnd->acName);
@ -499,11 +495,11 @@ main (int argc, const char *argv[])
const char *pcDump = argv[2];
const char *pcPayload = argv[3];
FILE *pfDump = NULL;
FILE *pfPayload = NULL;
FILE *pfDump = NULL;
FILE *pfPayload = NULL;
char abDump[4096];
char abPayload[4096];
char abDump[4096];
char abPayload[4096];
pfDump = fopen (pcDump, "rb");

View file

@ -55,11 +55,11 @@ print_success_or_failure (bool bFailure, uint32_t * uiCounter)
*uiCounter += (bFailure) ? 0 : 1;
}
static bool
static bool
read_card (void)
{
uint32_t page;
bool bFailure = false;
bool bFailure = false;
uint32_t uiReadedPages = 0;
printf ("Reading %d pages |", uiBlocks + 1);
@ -85,16 +85,16 @@ read_card (void)
return (!bFailure);
}
static bool
static bool
write_card (void)
{
uint32_t uiBlock = 0;
int page = 0x4;
bool bFailure = false;
int page = 0x4;
bool bFailure = false;
uint32_t uiWritenPages = 0;
char buffer[BUFSIZ];
bool write_otp;
char buffer[BUFSIZ];
bool write_otp;
printf ("Write OTP bytes ? [yN] ");
fgets (buffer, BUFSIZ, stdin);
@ -104,11 +104,11 @@ write_card (void)
printf ("Writing %d pages |", uiBlocks + 1);
printf ("sss");
if(write_otp) {
if (write_otp) {
page = 0x3;
} else {
/* If user don't want to write OTP, we skip 1 page more. */
printf("s");
printf ("s");
page = 0x4;
}
@ -122,7 +122,6 @@ write_card (void)
}
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
@ -130,12 +129,13 @@ write_card (void)
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;
bFailure = true;
print_success_or_failure (bFailure, &uiWritenPages);
}
printf ("|\n");
printf ("Done, %d of %d pages written (%d first pages are skipped).\n", uiWritenPages, uiBlocks + 1, write_otp?3:4);
printf ("Done, %d of %d pages written (%d first pages are skipped).\n", uiWritenPages, uiBlocks + 1,
write_otp ? 3 : 4);
return true;
}
@ -143,9 +143,9 @@ write_card (void)
int
main (int argc, const char *argv[])
{
bool bReadAction;
bool bReadAction;
byte_t *pbtUID;
FILE *pfDump;
FILE *pfDump;
if (argc < 3) {
printf ("\n");
@ -191,28 +191,26 @@ main (int argc, const char *argv[])
// Drop the field for a while
if (!nfc_configure (pnd, NDO_ACTIVATE_FIELD, false)) {
nfc_perror(pnd, "nfc_configure");
exit(EXIT_FAILURE);
nfc_perror (pnd, "nfc_configure");
exit (EXIT_FAILURE);
}
// Let the reader only try once to find a tag
if (!nfc_configure (pnd, NDO_INFINITE_SELECT, false)) {
nfc_perror(pnd, "nfc_configure");
exit(EXIT_FAILURE);
nfc_perror (pnd, "nfc_configure");
exit (EXIT_FAILURE);
}
if (!nfc_configure (pnd, NDO_HANDLE_CRC, true)) {
nfc_perror(pnd, "nfc_configure");
exit(EXIT_FAILURE);
nfc_perror (pnd, "nfc_configure");
exit (EXIT_FAILURE);
}
if (!nfc_configure (pnd, NDO_HANDLE_PARITY, true)) {
nfc_perror(pnd, "nfc_configure");
exit(EXIT_FAILURE);
nfc_perror (pnd, "nfc_configure");
exit (EXIT_FAILURE);
}
// Enable field so more power consuming cards can power themselves up
if (!nfc_configure (pnd, NDO_ACTIVATE_FIELD, true)) {
nfc_perror(pnd, "nfc_configure");
exit(EXIT_FAILURE);
nfc_perror (pnd, "nfc_configure");
exit (EXIT_FAILURE);
}
printf ("Connected to NFC reader: %s\n", pnd->acName);
@ -230,7 +228,6 @@ main (int argc, const char *argv[])
nfc_disconnect (pnd);
return EXIT_FAILURE;
}
// Get the info from the current tag (UID is stored little-endian)
pbtUID = nti.nai.abtUid;
printf ("Found MIFARE Ultralight card with UID: %02x%02x%02x%02x\n", pbtUID[3], pbtUID[2], pbtUID[1], pbtUID[0]);

View file

@ -44,8 +44,8 @@ static nfc_device_t *pnd;
int
main (int argc, const char *argv[])
{
size_t szFound;
size_t i;
size_t szFound;
size_t i;
nfc_device_desc_t *pnddDevices;
// Display libnfc version
@ -76,8 +76,8 @@ main (int argc, const char *argv[])
const size_t szTargetTypes = 1;
nfc_target_t antTargets[2];
size_t szTargetFound;
bool res;
size_t szTargetFound;
bool res;
pnd = nfc_connect (&(pnddDevices[i]));
@ -89,30 +89,27 @@ main (int argc, const char *argv[])
// Drop the field for a while
if (!nfc_configure (pnd, NDO_ACTIVATE_FIELD, false)) {
nfc_perror(pnd, "nfc_configure");
exit(EXIT_FAILURE);
nfc_perror (pnd, "nfc_configure");
exit (EXIT_FAILURE);
}
// Let the reader only try once to find a tag
if (!nfc_configure (pnd, NDO_INFINITE_SELECT, false)) {
nfc_perror(pnd, "nfc_configure");
exit(EXIT_FAILURE);
nfc_perror (pnd, "nfc_configure");
exit (EXIT_FAILURE);
}
// Configure the CRC and Parity settings
if (!nfc_configure (pnd, NDO_HANDLE_CRC, true)) {
nfc_perror(pnd, "nfc_configure");
exit(EXIT_FAILURE);
nfc_perror (pnd, "nfc_configure");
exit (EXIT_FAILURE);
}
if (!nfc_configure (pnd, NDO_HANDLE_PARITY, true)) {
nfc_perror(pnd, "nfc_configure");
exit(EXIT_FAILURE);
nfc_perror (pnd, "nfc_configure");
exit (EXIT_FAILURE);
}
// Enable field so more power consuming cards can power themselves up
if (!nfc_configure (pnd, NDO_ACTIVATE_FIELD, true)) {
nfc_perror(pnd, "nfc_configure");
exit(EXIT_FAILURE);
nfc_perror (pnd, "nfc_configure");
exit (EXIT_FAILURE);
}
printf ("Connected to NFC reader: %s\n", pnd->acName);

View file

@ -23,7 +23,7 @@
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
# include "config.h"
#endif /* HAVE_CONFIG_H */
#include <stdio.h>
@ -46,55 +46,58 @@ static size_t szReaderRxBits;
static byte_t abtTagRx[MAX_FRAME_LEN];
static byte_t abtTagRxPar[MAX_FRAME_LEN];
static size_t szTagRxBits;
static nfc_device_t* pndReader;
static nfc_device_t* pndTag;
static bool quitting=false;
static nfc_device_t *pndReader;
static nfc_device_t *pndTag;
static bool quitting = false;
void intr_hdlr(void)
void
intr_hdlr (void)
{
printf("\nQuitting...\n");
quitting=true;
printf ("\nQuitting...\n");
quitting = true;
return;
}
void print_usage(char* argv[])
void
print_usage (char *argv[])
{
printf("Usage: %s [OPTIONS]\n", argv[0]);
printf("Options:\n");
printf("\t-h\tHelp. Print this message.\n");
printf("\t-q\tQuiet mode. Suppress output of READER and EMULATOR data (improves timing).\n");
printf ("Usage: %s [OPTIONS]\n", argv[0]);
printf ("Options:\n");
printf ("\t-h\tHelp. Print this message.\n");
printf ("\t-q\tQuiet mode. Suppress output of READER and EMULATOR data (improves timing).\n");
}
int main(int argc,char* argv[])
int
main (int argc, char *argv[])
{
int arg;
bool quiet_output = false;
size_t szFound;
int arg;
bool quiet_output = false;
size_t szFound;
nfc_device_desc_t *pnddDevices;
const char* acLibnfcVersion = nfc_version();
const char *acLibnfcVersion = nfc_version ();
// Get commandline options
for (arg=1;arg<argc;arg++) {
if (0 == strcmp(argv[arg], "-h")) {
print_usage(argv);
for (arg = 1; arg < argc; arg++) {
if (0 == strcmp (argv[arg], "-h")) {
print_usage (argv);
return EXIT_SUCCESS;
} else if (0 == strcmp(argv[arg], "-q")) {
INFO("%s", "Quiet mode.");
} else if (0 == strcmp (argv[arg], "-q")) {
INFO ("%s", "Quiet mode.");
quiet_output = true;
} else {
ERR("%s is not supported option.", argv[arg]);
print_usage(argv);
ERR ("%s is not supported option.", argv[arg]);
print_usage (argv);
return EXIT_FAILURE;
}
}
// Display libnfc version
printf("%s use libnfc %s\n", argv[0], acLibnfcVersion);
printf ("%s use libnfc %s\n", argv[0], acLibnfcVersion);
#ifdef WIN32
signal(SIGINT, (void (__cdecl*)(int)) intr_hdlr);
signal (SIGINT, (void (__cdecl *) (int)) intr_hdlr);
#else
signal(SIGINT, (void (*)()) intr_hdlr);
signal (SIGINT, (void (*)()) intr_hdlr);
#endif
// Allocate memory to put the result of available devices listing
@ -102,105 +105,93 @@ int main(int argc,char* argv[])
fprintf (stderr, "malloc() failed\n");
return EXIT_FAILURE;
}
// List available devices
nfc_list_devices (pnddDevices, MAX_DEVICE_COUNT, &szFound);
if (szFound < 2) {
ERR("%zd device found but two connected devices are needed to relay NFC.", szFound);
ERR ("%zd device found but two connected devices are needed to relay NFC.", szFound);
return EXIT_FAILURE;
}
}
// Try to open the NFC emulator device
pndTag = nfc_connect(&(pnddDevices[0]));
if (pndTag == NULL)
{
printf("Error connecting NFC emulator device\n");
pndTag = nfc_connect (&(pnddDevices[0]));
if (pndTag == NULL) {
printf ("Error connecting NFC emulator device\n");
return EXIT_FAILURE;
}
printf("Hint: tag <---> emulator (relay) <---> reader (relay) <---> original reader\n\n");
printf("Connected to the NFC emulator device: %s\n", pndTag->acName);
printf("[+] Try to break out the auto-emulation, this requires a second reader!\n");
printf("[+] To do this, please send any command after the anti-collision\n");
printf("[+] For example, send a RATS command or use the \"nfc-anticol\" tool\n");
if (!nfc_target_init(pndTag,abtReaderRx,&szReaderRxBits))
{
ERR("%s", "Initialization of NFC emulator failed");
nfc_disconnect(pndTag);
printf ("Hint: tag <---> emulator (relay) <---> reader (relay) <---> original reader\n\n");
printf ("Connected to the NFC emulator device: %s\n", pndTag->acName);
printf ("[+] Try to break out the auto-emulation, this requires a second reader!\n");
printf ("[+] To do this, please send any command after the anti-collision\n");
printf ("[+] For example, send a RATS command or use the \"nfc-anticol\" tool\n");
if (!nfc_target_init (pndTag, abtReaderRx, &szReaderRxBits)) {
ERR ("%s", "Initialization of NFC emulator failed");
nfc_disconnect (pndTag);
return EXIT_FAILURE;
}
printf("%s", "Configuring emulator settings...");
if (!nfc_configure(pndTag,NDO_HANDLE_CRC,false) ||
!nfc_configure(pndTag,NDO_HANDLE_PARITY,false) ||
!nfc_configure(pndTag,NDO_ACCEPT_INVALID_FRAMES,true)) {
nfc_perror(pndTag, "nfc_configure");
exit(EXIT_FAILURE);
printf ("%s", "Configuring emulator settings...");
if (!nfc_configure (pndTag, NDO_HANDLE_CRC, false) ||
!nfc_configure (pndTag, NDO_HANDLE_PARITY, false) || !nfc_configure (pndTag, NDO_ACCEPT_INVALID_FRAMES, true)) {
nfc_perror (pndTag, "nfc_configure");
exit (EXIT_FAILURE);
}
printf("%s", "Done, emulated tag is initialized");
printf ("%s", "Done, emulated tag is initialized");
// Try to open the NFC reader
pndReader = nfc_connect(&(pnddDevices[1]));
printf("Connected to the NFC reader device: %s", pndReader->acName);
printf("%s", "Configuring NFC reader settings...");
nfc_initiator_init(pndReader);
if (!nfc_configure(pndReader,NDO_HANDLE_CRC,false) ||
!nfc_configure(pndReader,NDO_HANDLE_PARITY,false) ||
!nfc_configure(pndReader,NDO_ACCEPT_INVALID_FRAMES,true)) {
nfc_perror(pndReader, "nfc_configure");
exit(EXIT_FAILURE);
}
printf("%s", "Done, relaying frames now!");
pndReader = nfc_connect (&(pnddDevices[1]));
while(!quitting)
{
printf ("Connected to the NFC reader device: %s", pndReader->acName);
printf ("%s", "Configuring NFC reader settings...");
nfc_initiator_init (pndReader);
if (!nfc_configure (pndReader, NDO_HANDLE_CRC, false) ||
!nfc_configure (pndReader, NDO_HANDLE_PARITY, false) ||
!nfc_configure (pndReader, NDO_ACCEPT_INVALID_FRAMES, true)) {
nfc_perror (pndReader, "nfc_configure");
exit (EXIT_FAILURE);
}
printf ("%s", "Done, relaying frames now!");
while (!quitting) {
// Test if we received a frame from the reader
if (nfc_target_receive_bits(pndTag,abtReaderRx,&szReaderRxBits,abtReaderRxPar))
{
if (nfc_target_receive_bits (pndTag, abtReaderRx, &szReaderRxBits, abtReaderRxPar)) {
// Drop down the field before sending a REQA command and start a new session
if (szReaderRxBits == 7 && abtReaderRx[0] == 0x26)
{
if (szReaderRxBits == 7 && abtReaderRx[0] == 0x26) {
// Drop down field for a very short time (original tag will reboot)
if (!nfc_configure(pndReader,NDO_ACTIVATE_FIELD,false)) {
nfc_perror(pndReader, "nfc_configure");
exit(EXIT_FAILURE);
if (!nfc_configure (pndReader, NDO_ACTIVATE_FIELD, false)) {
nfc_perror (pndReader, "nfc_configure");
exit (EXIT_FAILURE);
}
if(!quiet_output)
printf("\n");
if (!nfc_configure(pndReader,NDO_ACTIVATE_FIELD,true)) {
nfc_perror(pndReader, "nfc_configure");
exit(EXIT_FAILURE);
if (!quiet_output)
printf ("\n");
if (!nfc_configure (pndReader, NDO_ACTIVATE_FIELD, true)) {
nfc_perror (pndReader, "nfc_configure");
exit (EXIT_FAILURE);
}
}
// Print the reader frame to the screen
if(!quiet_output)
{
printf("R: ");
print_hex_par(abtReaderRx,szReaderRxBits,abtReaderRxPar);
if (!quiet_output) {
printf ("R: ");
print_hex_par (abtReaderRx, szReaderRxBits, abtReaderRxPar);
}
// Forward the frame to the original tag
if (nfc_initiator_transceive_bits(pndReader,abtReaderRx,szReaderRxBits,abtReaderRxPar,abtTagRx,&szTagRxBits,abtTagRxPar))
{
if (nfc_initiator_transceive_bits
(pndReader, abtReaderRx, szReaderRxBits, abtReaderRxPar, abtTagRx, &szTagRxBits, abtTagRxPar)) {
// Redirect the answer back to the reader
if (!nfc_target_send_bits(pndTag,abtTagRx,szTagRxBits,abtTagRxPar)) {
nfc_perror(pndTag, "nfc_target_send_bits");
exit(EXIT_FAILURE);
if (!nfc_target_send_bits (pndTag, abtTagRx, szTagRxBits, abtTagRxPar)) {
nfc_perror (pndTag, "nfc_target_send_bits");
exit (EXIT_FAILURE);
}
// Print the tag frame to the screen
if(!quiet_output)
{
printf("T: ");
print_hex_par(abtTagRx,szTagRxBits,abtTagRxPar);
if (!quiet_output) {
printf ("T: ");
print_hex_par (abtTagRx, szTagRxBits, abtTagRxPar);
}
}
}
}
nfc_disconnect(pndTag);
nfc_disconnect(pndReader);
exit(EXIT_SUCCESS);
nfc_disconnect (pndTag);
nfc_disconnect (pndReader);
exit (EXIT_SUCCESS);
}

View file

@ -21,9 +21,9 @@
* @file nfc-sam.c
* @brief Configure the reader to comunicate with a SAM (Secure Access Module).
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
# include "config.h"
#endif // HAVE_CONFIG_H
#include <stdio.h>
@ -32,14 +32,14 @@
#ifndef _WIN32
// Needed by sleep() under Unix
#include <unistd.h>
#define sleep sleep
#define SUSP_TIME 1 // secs.
# include <unistd.h>
# define sleep sleep
# define SUSP_TIME 1 // secs.
#else
// Needed by Sleep() under Windows
#include <winbase.h>
#define sleep Sleep
#define SUSP_TIME 1000 // msecs.
# include <winbase.h>
# define sleep Sleep
# define SUSP_TIME 1000 // msecs.
#endif
#include <nfc/nfc.h>
@ -49,183 +49,178 @@
#include "chips/pn53x.h"
#define MAX_FRAME_LEN 264
#define TIMEOUT 60 // secs.
#define TIMEOUT 60 // secs.
#define NORMAL_MODE 1
#define VIRTUAL_CARD_MODE 2
#define WIRED_CARD_MODE 3
#define DUAL_CARD_MODE 4
bool sam_connection(nfc_device_t* pnd, int mode)
bool
sam_connection (nfc_device_t * pnd, int mode)
{
byte_t pncmd_sam_config[] = { 0xD4,0x14,0x00,0x00 };
size_t szCmd = 0;
byte_t abtRx[MAX_FRAME_LEN];
size_t szRxLen;
byte_t pncmd_sam_config[] = { 0xD4, 0x14, 0x00, 0x00 };
size_t szCmd = 0;
byte_t abtRx[MAX_FRAME_LEN];
size_t szRxLen;
pncmd_sam_config[2] = mode;
switch (mode)
{
case VIRTUAL_CARD_MODE:
switch (mode) {
case VIRTUAL_CARD_MODE:
{
// Only the VIRTUAL_CARD_MODE requires 4 bytes.
szCmd = sizeof(pncmd_sam_config);
szCmd = sizeof (pncmd_sam_config);
}
break;
default:
default:
{
szCmd = sizeof(pncmd_sam_config)-1;
szCmd = sizeof (pncmd_sam_config) - 1;
}
break;
}
// FIXME: Direct call
if (!pn53x_transceive(pnd,pncmd_sam_config,szCmd,abtRx,&szRxLen)) {
ERR("%s %d", "Unable to execute SAMConfiguration command with mode byte:", mode);
if (!pn53x_transceive (pnd, pncmd_sam_config, szCmd, abtRx, &szRxLen)) {
ERR ("%s %d", "Unable to execute SAMConfiguration command with mode byte:", mode);
return false;
}
return true;
}
void wait_one_minute()
void
wait_one_minute ()
{
int secs = 0;
printf("|");
fflush(stdout);
while (secs < TIMEOUT)
{
sleep(SUSP_TIME);
int secs = 0;
printf ("|");
fflush (stdout);
while (secs < TIMEOUT) {
sleep (SUSP_TIME);
secs++;
printf(".");
fflush(stdout);
printf (".");
fflush (stdout);
}
printf("|\n");
printf ("|\n");
}
int main(int argc, const char* argv[])
int
main (int argc, const char *argv[])
{
nfc_device_t* pnd;
nfc_device_t *pnd;
(void)argc;
(void)argv;
(void) argc;
(void) argv;
// Display libnfc version
const char* acLibnfcVersion = nfc_version();
printf("%s use libnfc %s\n", argv[0], acLibnfcVersion);
const char *acLibnfcVersion = nfc_version ();
printf ("%s use libnfc %s\n", argv[0], acLibnfcVersion);
// Connect using the first available NFC device
pnd = nfc_connect(NULL);
pnd = nfc_connect (NULL);
if (pnd == NULL) {
ERR("%s", "Unable to connect to NFC device.");
ERR ("%s", "Unable to connect to NFC device.");
return EXIT_FAILURE;
}
printf("Connected to NFC reader: %s\n",pnd->acName);
printf ("Connected to NFC reader: %s\n", pnd->acName);
// Print the example's menu
printf("\nSelect the comunication mode:\n");
printf("[1] Virtual card mode.\n");
printf("[2] Wired card mode.\n");
printf("[3] Dual card mode.\n");
printf(">> ");
printf ("\nSelect the comunication 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
char input = getchar();
int mode = input-'0'+1;
printf("\n");
char input = getchar ();
int mode = input - '0' + 1;
printf ("\n");
if (mode < VIRTUAL_CARD_MODE || mode > DUAL_CARD_MODE) {
ERR("%s", "Invalid selection.");
ERR ("%s", "Invalid selection.");
return EXIT_FAILURE;
}
// Connect with the SAM
sam_connection(pnd, mode);
switch (mode)
{
case VIRTUAL_CARD_MODE:
sam_connection (pnd, mode);
switch (mode) {
case VIRTUAL_CARD_MODE:
{
// FIXME: after the loop the reader doesn't respond to host commands...
printf("Now the SAM is readable for 1 minute from an external reader.\n");
wait_one_minute();
printf ("Now the SAM is readable for 1 minute from an external reader.\n");
wait_one_minute ();
}
break;
case WIRED_CARD_MODE:
case WIRED_CARD_MODE:
{
nfc_target_info_t nti;
// Set connected NFC device to initiator mode
nfc_initiator_init(pnd);
nfc_initiator_init (pnd);
// Drop the field for a while
if (!nfc_configure(pnd,NDO_ACTIVATE_FIELD,false)) {
nfc_perror(pnd, "nfc_configure");
exit(EXIT_FAILURE);
if (!nfc_configure (pnd, NDO_ACTIVATE_FIELD, false)) {
nfc_perror (pnd, "nfc_configure");
exit (EXIT_FAILURE);
}
// Let the reader only try once to find a tag
if (!nfc_configure(pnd,NDO_INFINITE_SELECT,false)) {
nfc_perror(pnd, "nfc_configure");
exit(EXIT_FAILURE);
if (!nfc_configure (pnd, NDO_INFINITE_SELECT, false)) {
nfc_perror (pnd, "nfc_configure");
exit (EXIT_FAILURE);
}
// Configure the CRC and Parity settings
if (!nfc_configure(pnd,NDO_HANDLE_CRC,true)) {
nfc_perror(pnd, "nfc_configure");
exit(EXIT_FAILURE);
if (!nfc_configure (pnd, NDO_HANDLE_CRC, true)) {
nfc_perror (pnd, "nfc_configure");
exit (EXIT_FAILURE);
}
if (!nfc_configure(pnd,NDO_HANDLE_PARITY,true)) {
nfc_perror(pnd, "nfc_configure");
exit(EXIT_FAILURE);
if (!nfc_configure (pnd, NDO_HANDLE_PARITY, true)) {
nfc_perror (pnd, "nfc_configure");
exit (EXIT_FAILURE);
}
// Enable field so more power consuming cards can power themselves up
if (!nfc_configure(pnd,NDO_ACTIVATE_FIELD,true)) {
nfc_perror(pnd, "nfc_configure");
exit(EXIT_FAILURE);
if (!nfc_configure (pnd, NDO_ACTIVATE_FIELD, true)) {
nfc_perror (pnd, "nfc_configure");
exit (EXIT_FAILURE);
}
// Read the SAM's info
if (!nfc_initiator_select_passive_target(pnd,NM_ISO14443A_106,NULL,0,&nti)) {
ERR("%s", "Reading of SAM info failed.");
if (!nfc_initiator_select_passive_target (pnd, NM_ISO14443A_106, NULL, 0, &nti)) {
ERR ("%s", "Reading of SAM info failed.");
return EXIT_FAILURE;
}
printf("The following ISO14443A tag (SAM) was found:\n\n");
printf ("The following ISO14443A tag (SAM) was found:\n\n");
print_nfc_iso14443a_info (nti.nai);
}
break;
case DUAL_CARD_MODE:
case DUAL_CARD_MODE:
{
byte_t abtRx[MAX_FRAME_LEN];
size_t szRxLen;
byte_t abtRx[MAX_FRAME_LEN];
size_t szRxLen;
// FIXME: it does not work as expected...Probably the issue is in "nfc_target_init"
// which doesn't provide a way to set custom data for SENS_RES, NFCID1, SEL_RES, etc.
if (!nfc_target_init(pnd,abtRx,&szRxLen))
if (!nfc_target_init (pnd, abtRx, &szRxLen))
return EXIT_FAILURE;
printf("Now both the NFC reader and SAM are readable for 1 minute from an external reader.\n");
wait_one_minute();
printf ("Now both the NFC reader and SAM are readable for 1 minute from an external reader.\n");
wait_one_minute ();
}
break;
}
// Disconnect from the SAM
sam_connection(pnd, NORMAL_MODE);
sam_connection (pnd, NORMAL_MODE);
// Disconnect from NFC device
nfc_disconnect(pnd);
nfc_disconnect (pnd);
return EXIT_SUCCESS;
}

View file

@ -21,152 +21,165 @@ static const byte_t OddParity[256] = {
1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1
};
byte_t oddparity(const byte_t bt)
byte_t
oddparity (const byte_t bt)
{
return OddParity[bt];
}
void oddparity_bytes_ts(const byte_t* pbtData, const size_t szLen, byte_t* pbtPar)
void
oddparity_bytes_ts (const byte_t * pbtData, const size_t szLen, byte_t * pbtPar)
{
size_t szByteNr;
size_t szByteNr;
// Calculate the parity bits for the command
for (szByteNr=0; szByteNr<szLen; szByteNr++)
{
for (szByteNr = 0; szByteNr < szLen; szByteNr++) {
pbtPar[szByteNr] = OddParity[pbtData[szByteNr]];
}
}
void print_hex(const byte_t* pbtData, const size_t szBytes)
void
print_hex (const byte_t * pbtData, const size_t szBytes)
{
size_t szPos;
size_t szPos;
for (szPos=0; szPos < szBytes; szPos++)
{
printf("%02x ",pbtData[szPos]);
for (szPos = 0; szPos < szBytes; szPos++) {
printf ("%02x ", pbtData[szPos]);
}
printf("\n");
printf ("\n");
}
void print_hex_bits(const byte_t* pbtData, const size_t szBits)
void
print_hex_bits (const byte_t * pbtData, const size_t szBits)
{
uint8_t uRemainder;
size_t szPos;
size_t szBytes = szBits/8;
size_t szPos;
size_t szBytes = szBits / 8;
for (szPos=0; szPos < szBytes; szPos++)
{
printf("%02x ",pbtData[szPos]);
for (szPos = 0; szPos < szBytes; szPos++) {
printf ("%02x ", pbtData[szPos]);
}
uRemainder = szBits % 8;
// Print the rest bits
if (uRemainder != 0)
{
if (uRemainder != 0) {
if (uRemainder < 5)
printf("%01x (%d bits)",pbtData[szBytes], uRemainder);
printf ("%01x (%d bits)", pbtData[szBytes], uRemainder);
else
printf("%02x (%d bits)",pbtData[szBytes], uRemainder);
printf ("%02x (%d bits)", pbtData[szBytes], uRemainder);
}
printf("\n");
printf ("\n");
}
void print_hex_par(const byte_t* pbtData, const size_t szBits, const byte_t* pbtDataPar)
void
print_hex_par (const byte_t * pbtData, const size_t szBits, const byte_t * pbtDataPar)
{
uint8_t uRemainder;
size_t szPos;
size_t szBytes = szBits/8;
size_t szPos;
size_t szBytes = szBits / 8;
for (szPos=0; szPos < szBytes; szPos++)
{
printf("%02x",pbtData[szPos]);
if (OddParity[pbtData[szPos]] != pbtDataPar[szPos])
{
printf("! ");
for (szPos = 0; szPos < szBytes; szPos++) {
printf ("%02x", pbtData[szPos]);
if (OddParity[pbtData[szPos]] != pbtDataPar[szPos]) {
printf ("! ");
} else {
printf(" ");
printf (" ");
}
}
uRemainder = szBits % 8;
// Print the rest bits, these cannot have parity bit
if (uRemainder != 0)
{
if (uRemainder != 0) {
if (uRemainder < 5)
printf("%01x (%d bits)",pbtData[szBytes], uRemainder);
printf ("%01x (%d bits)", pbtData[szBytes], uRemainder);
else
printf("%02x (%d bits)",pbtData[szBytes], uRemainder);
printf ("%02x (%d bits)", pbtData[szBytes], uRemainder);
}
printf("\n");
printf ("\n");
}
#define SAK_ISO14443_4_COMPLIANT 0x20
#define SAK_ISO18092_COMPLIANT 0x40
void print_nfc_iso14443a_info(const nfc_iso14443a_info_t nai)
void
print_nfc_iso14443a_info (const nfc_iso14443a_info_t nai)
{
printf(" ATQA (SENS_RES): "); print_hex(nai.abtAtqa,2);
printf(" UID (NFCID%c): ",(nai.abtUid[0]==0x08?'3':'1')); print_hex(nai.abtUid, nai.szUidLen);
printf(" SAK (SEL_RES): "); print_hex(&nai.btSak,1);
printf (" ATQA (SENS_RES): ");
print_hex (nai.abtAtqa, 2);
printf (" UID (NFCID%c): ", (nai.abtUid[0] == 0x08 ? '3' : '1'));
print_hex (nai.abtUid, nai.szUidLen);
printf (" SAK (SEL_RES): ");
print_hex (&nai.btSak, 1);
if (nai.szAtsLen) {
printf(" ATS (ATR): ");
print_hex(nai.abtAts, nai.szAtsLen);
printf (" ATS (ATR): ");
print_hex (nai.abtAts, nai.szAtsLen);
}
if ( (nai.btSak & SAK_ISO14443_4_COMPLIANT) || (nai.btSak & SAK_ISO18092_COMPLIANT) ) {
printf(" Compliant with: ");
if (nai.btSak & SAK_ISO14443_4_COMPLIANT) printf("ISO/IEC 14443-4 ");
if (nai.btSak & SAK_ISO18092_COMPLIANT) printf("ISO/IEC 18092");
printf("\n");
if ((nai.btSak & SAK_ISO14443_4_COMPLIANT) || (nai.btSak & SAK_ISO18092_COMPLIANT)) {
printf (" Compliant with: ");
if (nai.btSak & SAK_ISO14443_4_COMPLIANT)
printf ("ISO/IEC 14443-4 ");
if (nai.btSak & SAK_ISO18092_COMPLIANT)
printf ("ISO/IEC 18092");
printf ("\n");
}
}
void print_nfc_felica_info(const nfc_felica_info_t nfi)
{
printf(" ID (NFCID2): "); print_hex(nfi.abtId,8);
printf(" Parameter (PAD): "); print_hex(nfi.abtPad,8);
}
void print_nfc_iso14443b_info(const nfc_iso14443b_info_t nbi)
void
print_nfc_felica_info (const nfc_felica_info_t nfi)
{
printf(" ATQB: "); print_hex(nbi.abtAtqb,12);
printf(" ID: "); print_hex(nbi.abtId,4);
printf(" CID: %02x\n",nbi.btCid);
if (nbi.szInfLen>0) {
printf(" INF: "); print_hex(nbi.abtInf,nbi.szInfLen);
printf (" ID (NFCID2): ");
print_hex (nfi.abtId, 8);
printf (" Parameter (PAD): ");
print_hex (nfi.abtPad, 8);
}
void
print_nfc_iso14443b_info (const nfc_iso14443b_info_t nbi)
{
printf (" ATQB: ");
print_hex (nbi.abtAtqb, 12);
printf (" ID: ");
print_hex (nbi.abtId, 4);
printf (" CID: %02x\n", nbi.btCid);
if (nbi.szInfLen > 0) {
printf (" INF: ");
print_hex (nbi.abtInf, nbi.szInfLen);
}
printf(" PARAMS: %02x %02x %02x %02x\n",nbi.btParam1,nbi.btParam2,nbi.btParam3,nbi.btParam4);
printf (" PARAMS: %02x %02x %02x %02x\n", nbi.btParam1, nbi.btParam2, nbi.btParam3, nbi.btParam4);
}
/**
* @brief Tries to parse arguments to find device descriptions.
* @return Returns the list of found device descriptions.
*/
nfc_device_desc_t* parse_device_desc(int argc, const char *argv[], size_t* szFound)
nfc_device_desc_t *
parse_device_desc (int argc, const char *argv[], size_t * szFound)
{
nfc_device_desc_t* pndd = 0;
int arg;
nfc_device_desc_t *pndd = 0;
int arg;
*szFound = 0;
// Get commandline options
for (arg=1;arg<argc;arg++) {
for (arg = 1; arg < argc; arg++) {
if (0 == strcmp(argv[arg], "--device")) {
if (0 == strcmp (argv[arg], "--device")) {
if (argc > arg+1) {
char buffer[256];
if (argc > arg + 1) {
char buffer[256];
pndd = malloc(sizeof(nfc_device_desc_t));
pndd = malloc (sizeof (nfc_device_desc_t));
strncpy(buffer, argv[++arg], 256);
strncpy (buffer, argv[++arg], 256);
// Driver.
pndd->pcDriver = (char *)malloc(256);
strcpy(pndd->pcDriver, strtok(buffer, ":"));
pndd->pcDriver = (char *) malloc (256);
strcpy (pndd->pcDriver, strtok (buffer, ":"));
// Port.
pndd->pcPort = (char *)malloc(256);
strcpy(pndd->pcPort, strtok(NULL, ":"));
pndd->pcPort = (char *) malloc (256);
strcpy (pndd->pcPort, strtok (NULL, ":"));
// Speed.
sscanf(strtok(NULL, ":"), "%u", &pndd->uiSpeed);
sscanf (strtok (NULL, ":"), "%u", &pndd->uiSpeed);
*szFound = 1;
}
@ -176,4 +189,3 @@ nfc_device_desc_t* parse_device_desc(int argc, const char *argv[], size_t* szFou
return pndd;
}

View file

@ -22,22 +22,22 @@
*/
#ifndef _EXAMPLES_NFC_UTILS_H_
#define _EXAMPLES_NFC_UTILS_H_
# define _EXAMPLES_NFC_UTILS_H_
#include <stdlib.h>
#include <string.h>
# include <stdlib.h>
# include <string.h>
byte_t oddparity(const byte_t bt);
void oddparity_byte_ts(const byte_t* pbtData, const size_t szLen, byte_t* pbtPar);
byte_t oddparity (const byte_t bt);
void oddparity_byte_ts (const byte_t * pbtData, const size_t szLen, byte_t * pbtPar);
void print_hex(const byte_t* pbtData, const size_t szLen);
void print_hex_bits(const byte_t* pbtData, const size_t szBits);
void print_hex_par(const byte_t* pbtData, const size_t szBits, const byte_t* pbtDataPar);
void print_hex (const byte_t * pbtData, const size_t szLen);
void print_hex_bits (const byte_t * pbtData, const size_t szBits);
void print_hex_par (const byte_t * pbtData, const size_t szBits, const byte_t * pbtDataPar);
void print_nfc_iso14443a_info(const nfc_iso14443a_info_t nai);
void print_nfc_iso14443b_info(const nfc_iso14443b_info_t nbi);
void print_nfc_felica_info(const nfc_felica_info_t nfi);
void print_nfc_iso14443a_info (const nfc_iso14443a_info_t nai);
void print_nfc_iso14443b_info (const nfc_iso14443b_info_t nbi);
void print_nfc_felica_info (const nfc_felica_info_t nfi);
nfc_device_desc_t* parse_device_desc(int argc, const char *argv[], size_t* szFound);
nfc_device_desc_t *parse_device_desc (int argc, const char *argv[], size_t * szFound);
#endif

View file

@ -23,7 +23,7 @@
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
# include "config.h"
#endif // HAVE_CONFIG_H
#include <err.h>
@ -33,40 +33,36 @@
#define MAX_FRAME_LEN 264
int main(int argc, const char *argv[])
int
main (int argc, const char *argv[])
{
nfc_device_t *pnd;
nfc_target_info_t ti;
byte_t abtRecv[MAX_FRAME_LEN];
size_t szRecvBits;
byte_t send[] = "Hello World!";
byte_t abtRecv[MAX_FRAME_LEN];
size_t szRecvBits;
byte_t send[] = "Hello World!";
if (argc > 1) {
errx (1, "usage: %s", argv[0]);
}
pnd = nfc_connect(NULL);
if (!pnd || !nfc_initiator_init(pnd)
|| !nfc_initiator_select_dep_target(pnd, NM_PASSIVE_DEP, NULL, 0,
NULL, 0, NULL, 0, &ti)) {
printf
("unable to connect, initialize, or select the target\n");
pnd = nfc_connect (NULL);
if (!pnd || !nfc_initiator_init (pnd)
|| !nfc_initiator_select_dep_target (pnd, NM_PASSIVE_DEP, NULL, 0, NULL, 0, NULL, 0, &ti)) {
printf ("unable to connect, initialize, or select the target\n");
return 1;
}
printf("Sending : %s\n", send);
if (!nfc_initiator_transceive_bytes(pnd,
send,
strlen((char*)send), abtRecv,
&szRecvBits)) {
printf("unable to send data\n");
printf ("Sending : %s\n", send);
if (!nfc_initiator_transceive_bytes (pnd, send, strlen ((char *) send), abtRecv, &szRecvBits)) {
printf ("unable to send data\n");
return 1;
}
abtRecv[szRecvBits] = 0;
printf("Received: %s\n", abtRecv);
printf ("Received: %s\n", abtRecv);
nfc_initiator_deselect_target(pnd);
nfc_disconnect(pnd);
nfc_initiator_deselect_target (pnd);
nfc_disconnect (pnd);
return 0;
}

View file

@ -24,7 +24,7 @@
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
# include "config.h"
#endif // HAVE_CONFIG_H
#include <err.h>
@ -33,35 +33,36 @@
#define MAX_FRAME_LEN 264
int main(int argc, const char *argv[])
int
main (int argc, const char *argv[])
{
byte_t abtRecv[MAX_FRAME_LEN];
size_t szRecvBits;
byte_t send[] = "Hello Mars!";
nfc_device_t *pnd = nfc_connect(NULL);
byte_t abtRecv[MAX_FRAME_LEN];
size_t szRecvBits;
byte_t send[] = "Hello Mars!";
nfc_device_t *pnd = nfc_connect (NULL);
if (argc > 1) {
errx (1, "usage: %s", argv[0]);
}
if (!pnd || !nfc_target_init(pnd, abtRecv, &szRecvBits)) {
printf("unable to connect or initialize\n");
if (!pnd || !nfc_target_init (pnd, abtRecv, &szRecvBits)) {
printf ("unable to connect or initialize\n");
return 1;
}
if (!nfc_target_receive_bytes(pnd, abtRecv, &szRecvBits)) {
printf("unable to receive data\n");
if (!nfc_target_receive_bytes (pnd, abtRecv, &szRecvBits)) {
printf ("unable to receive data\n");
return 1;
}
abtRecv[szRecvBits] = 0;
printf("Received: %s\n", abtRecv);
printf("Sending : %s\n", send);
printf ("Received: %s\n", abtRecv);
printf ("Sending : %s\n", send);
if (!nfc_target_send_bytes(pnd, send, 11)) {
printf("unable to send data\n");
if (!nfc_target_send_bytes (pnd, send, 11)) {
printf ("unable to send data\n");
return 1;
}
nfc_disconnect(pnd);
nfc_disconnect (pnd);
return 0;
}

View file

@ -35,17 +35,18 @@
#define MAX_DEVICE_COUNT 16
int main(int argc, const char* argv[])
int
main (int argc, const char *argv[])
{
size_t szFound;
size_t i;
nfc_device_t* pnd;
size_t szFound;
size_t i;
nfc_device_t *pnd;
nfc_device_desc_t *pnddDevices;
const char* acLibnfcVersion;
bool result;
const char *acLibnfcVersion;
bool result;
byte_t abtRx[MAX_FRAME_LEN];
size_t szRxLen;
byte_t abtRx[MAX_FRAME_LEN];
size_t szRxLen;
const byte_t pncmd_diagnose_communication_line_test[] = { 0xD4, 0x00, 0x00, 0x06, 'l', 'i', 'b', 'n', 'f', 'c' };
const byte_t pncmd_diagnose_rom_test[] = { 0xD4, 0x00, 0x01 };
const byte_t pncmd_diagnose_ram_test[] = { 0xD4, 0x00, 0x02 };
@ -53,10 +54,9 @@ int main(int argc, const char* argv[])
if (argc > 1) {
errx (1, "usage: %s", argv[0]);
}
// Display libnfc version
acLibnfcVersion = nfc_version();
printf("%s use libnfc %s\n", argv[0], acLibnfcVersion);
acLibnfcVersion = nfc_version ();
printf ("%s use libnfc %s\n", argv[0], acLibnfcVersion);
if (!(pnddDevices = malloc (MAX_DEVICE_COUNT * sizeof (*pnddDevices)))) {
fprintf (stderr, "malloc() failed\n");
@ -66,38 +66,42 @@ int main(int argc, const char* argv[])
nfc_list_devices (pnddDevices, MAX_DEVICE_COUNT, &szFound);
if (szFound == 0) {
INFO("%s", "No device found.");
INFO ("%s", "No device found.");
}
for (i = 0; i < szFound; i++) {
pnd = nfc_connect(&(pnddDevices[i]));
pnd = nfc_connect (&(pnddDevices[i]));
if (pnd == NULL) {
ERR("%s", "Unable to connect to NFC device.");
ERR ("%s", "Unable to connect to NFC device.");
return EXIT_FAILURE;
}
printf("NFC device [%s] connected.\n",pnd->acName);
printf ("NFC device [%s] connected.\n", pnd->acName);
// FIXME: Direct call
result = pn53x_transceive(pnd,pncmd_diagnose_communication_line_test,sizeof(pncmd_diagnose_communication_line_test),abtRx,&szRxLen);
if ( result ) {
result = (memcmp(pncmd_diagnose_communication_line_test+2, abtRx, sizeof(pncmd_diagnose_communication_line_test)-2 ) == 0);
result =
pn53x_transceive (pnd, pncmd_diagnose_communication_line_test, sizeof (pncmd_diagnose_communication_line_test),
abtRx, &szRxLen);
if (result) {
result =
(memcmp (pncmd_diagnose_communication_line_test + 2, abtRx, sizeof (pncmd_diagnose_communication_line_test) - 2)
== 0);
}
printf(" Communication line test: %s\n", result ? "OK" : "Failed");
printf (" Communication line test: %s\n", result ? "OK" : "Failed");
// FIXME: Direct call
result = pn53x_transceive(pnd,pncmd_diagnose_rom_test,sizeof(pncmd_diagnose_rom_test),abtRx,&szRxLen);
if ( result ) {
result = pn53x_transceive (pnd, pncmd_diagnose_rom_test, sizeof (pncmd_diagnose_rom_test), abtRx, &szRxLen);
if (result) {
result = ((szRxLen == 1) && (abtRx[0] == 0x00));
}
printf(" ROM test: %s\n", result ? "OK" : "Failed");
printf (" ROM test: %s\n", result ? "OK" : "Failed");
// FIXME: Direct call
result = pn53x_transceive(pnd,pncmd_diagnose_ram_test,sizeof(pncmd_diagnose_ram_test),abtRx,&szRxLen);
if ( result ) {
result = pn53x_transceive (pnd, pncmd_diagnose_ram_test, sizeof (pncmd_diagnose_ram_test), abtRx, &szRxLen);
if (result) {
result = ((szRxLen == 1) && (abtRx[0] == 0x00));
}
printf(" RAM test: %s\n", result ? "OK" : "Failed");
printf (" RAM test: %s\n", result ? "OK" : "Failed");
}
}