update to use libnfc's trunk

This commit is contained in:
Audrey Diacre 2012-01-26 09:24:21 +00:00
parent ea0c8a7047
commit 5c62782645
6 changed files with 179 additions and 214 deletions

View file

@ -50,7 +50,7 @@
#include "mfoc.h"
int main(int argc, char * const argv[]) {
const nfc_modulation_t nm = {
const nfc_modulation nm = {
.nmt = NMT_ISO14443A,
.nbr = NBR_106,
};
@ -70,11 +70,11 @@ int main(int argc, char * const argv[]) {
bool skip = false;
// Next default key specified as option (-k)
byte_t * defKeys = NULL, *p;
uint8_t * defKeys = NULL, *p;
size_t defKeys_len = 0;
// Array with default Mifare Classic keys
byte_t defaultKeys[][6] = {
uint8_t defaultKeys[][6] = {
{0xff, 0xff, 0xff, 0xff, 0xff, 0xff}, // Default key (first key used by program if no user defined key)
{0xa0, 0xa1, 0xa2, 0xa3, 0xa4, 0xa5}, // NFCForum MAD key
{0xd3, 0xf7, 0xd3, 0xf7, 0xd3, 0xf7}, // NFCForum content key
@ -166,27 +166,27 @@ int main(int argc, char * const argv[]) {
// Initialize reader/tag structures
mf_init(&t, &r);
if (!nfc_initiator_init (r.pdi)) {
if (nfc_initiator_init (r.pdi) < 0) {
nfc_perror (r.pdi, "nfc_initiator_init");
goto error;
}
// Drop the field for a while, so can be reset
if (!nfc_configure(r.pdi, NDO_ACTIVATE_FIELD, true)) {
nfc_perror (r.pdi, "nfc_configure activate field");
if (nfc_device_set_property_bool(r.pdi, NP_ACTIVATE_FIELD, true) < 0) {
nfc_perror (r.pdi, "nfc_device_set_property_bool activate field");
goto error;
}
// Let the reader only try once to find a tag
if (!nfc_configure(r.pdi, NDO_INFINITE_SELECT, false)) {
nfc_perror (r.pdi, "nfc_configure infinite select");
if (nfc_device_set_property_bool(r.pdi, NP_INFINITE_SELECT, false) < 0) {
nfc_perror (r.pdi, "nfc_device_set_property_bool infinite select");
goto error;
}
// Configure the CRC and Parity settings
if (!nfc_configure(r.pdi, NDO_HANDLE_CRC, true)) {
nfc_perror (r.pdi, "nfc_configure crc");
if (nfc_device_set_property_bool(r.pdi, NP_HANDLE_CRC, true) < 0) {
nfc_perror (r.pdi, "nfc_device_set_property_bool crc");
goto error;
}
if (!nfc_configure(r.pdi, NDO_HANDLE_PARITY, true)) {
nfc_perror (r.pdi, "nfc_configure parity");
if (nfc_device_set_property_bool(r.pdi, NP_HANDLE_PARITY, true) < 0) {
nfc_perror (r.pdi, "nfc_device_set_property_bool parity");
goto error;
}
@ -196,7 +196,7 @@ int main(int argc, char * const argv[]) {
*/
// mf_select_tag(r.pdi, &(t.nt));
if (!nfc_initiator_select_passive_target (r.pdi, nm, NULL, 0, &t.nt)) {
if (nfc_initiator_select_passive_target (r.pdi, nm, NULL, 0, &t.nt) < 0) {
nfc_perror (r.pdi, "nfc_initiator_select_passive_target");
goto error;
}
@ -507,14 +507,16 @@ int main(int argc, char * const argv[]) {
free(d.distances);
// Reset the "advanced" configuration to normal
nfc_configure(r.pdi, NDO_HANDLE_CRC, true);
nfc_configure(r.pdi, NDO_HANDLE_PARITY, true);
nfc_device_set_property_bool(r.pdi, NP_HANDLE_CRC, true);
nfc_device_set_property_bool(r.pdi, NP_HANDLE_PARITY, true);
// Disconnect device and exit
nfc_disconnect(r.pdi);
nfc_close(r.pdi);
nfc_exit(NULL);
exit (EXIT_SUCCESS);
error:
nfc_disconnect(r.pdi);
nfc_close(r.pdi);
nfc_exit(NULL);
exit (EXIT_FAILURE);
}
@ -542,53 +544,55 @@ void usage(FILE * stream, int errno) {
void mf_init(mftag *t, mfreader *r) {
// Connect to the first NFC device
r->pdi = nfc_connect(NULL);
nfc_init(NULL);
r->pdi = nfc_open(NULL, NULL);
if (!r->pdi) {
printf ("No NFC device found.\n");
exit (EXIT_FAILURE);
}
}
void mf_configure(nfc_device_t* pdi) {
if (!nfc_initiator_init (pdi)) {
void mf_configure(nfc_device* pdi) {
if (nfc_initiator_init (pdi) < 0) {
nfc_perror (pdi, "nfc_initiator_init");
exit (EXIT_FAILURE);
}
// Drop the field for a while, so can be reset
if (!nfc_configure(pdi, NDO_ACTIVATE_FIELD, false)) {
nfc_perror (pdi, "nfc_configure activate field");
if (nfc_device_set_property_bool(pdi, NP_ACTIVATE_FIELD, false) < 0) {
nfc_perror (pdi, "nfc_device_set_property_bool activate field");
exit (EXIT_FAILURE);
}
// Let the reader only try once to find a tag
if (!nfc_configure(pdi, NDO_INFINITE_SELECT, false)) {
nfc_perror (pdi, "nfc_configure infinite select");
if (nfc_device_set_property_bool(pdi, NP_INFINITE_SELECT, false) < 0) {
nfc_perror (pdi, "nfc_device_set_property_bool infinite select");
exit (EXIT_FAILURE);
}
// Configure the CRC and Parity settings
if (!nfc_configure(pdi, NDO_HANDLE_CRC, true)) {
nfc_perror (pdi, "nfc_configure crc");
if (nfc_device_set_property_bool(pdi, NP_HANDLE_CRC, true) < 0) {
nfc_perror (pdi, "nfc_device_set_property_bool crc");
exit (EXIT_FAILURE);
}
if (!nfc_configure(pdi, NDO_HANDLE_PARITY, true)) {
nfc_perror (pdi, "nfc_configure parity");
if (nfc_device_set_property_bool(pdi, NP_HANDLE_PARITY, true) < 0) {
nfc_perror (pdi, "nfc_device_set_property_bool parity");
exit (EXIT_FAILURE);
}
// Enable the field so more power consuming cards can power themselves up
if (!nfc_configure(pdi, NDO_ACTIVATE_FIELD, true)) {
nfc_perror (pdi, "nfc_configure activate field");
if (nfc_device_set_property_bool(pdi, NP_ACTIVATE_FIELD, true) < 0) {
nfc_perror (pdi, "nfc_device_set_property_bool activate field");
exit (EXIT_FAILURE);
}
}
void mf_select_tag(nfc_device_t* pdi, nfc_target_t* pnt) {
void mf_select_tag(nfc_device* pdi, nfc_target* pnt) {
// Poll for a ISO14443A (MIFARE) tag
const nfc_modulation_t nm = {
const nfc_modulation nm = {
.nmt = NMT_ISO14443A,
.nbr = NBR_106,
};
if (!nfc_initiator_select_passive_target(pdi, nm, NULL, 0, pnt)) {
if (nfc_initiator_select_passive_target(pdi, nm, NULL, 0, pnt) < 0) {
ERR ("Unable to connect to the MIFARE Classic tag");
nfc_disconnect(pdi);
nfc_close(pdi);
nfc_exit(NULL);
exit (EXIT_FAILURE);
}
}
@ -625,11 +629,11 @@ int find_exploit_sector(mftag t) {
}
void mf_anticollision(mftag t, mfreader r) {
const nfc_modulation_t nm = {
const nfc_modulation nm = {
.nmt = NMT_ISO14443A,
.nbr = NBR_106,
};
if (!nfc_initiator_select_passive_target(r.pdi, nm, NULL, 0, &t.nt)) {
if (nfc_initiator_select_passive_target(r.pdi, nm, NULL, 0, &t.nt) < 0) {
nfc_perror (r.pdi, "nfc_initiator_select_passive_target");
ERR ("Tag has been removed");
exit (EXIT_FAILURE);
@ -646,16 +650,16 @@ int mf_enhanced_auth(int e_sector, int a_sector, mftag t, mfreader r, denonce *d
// Possible key counter, just continue with a previous "session"
uint32_t kcount = pk->size;
byte_t Nr[4] = { 0x00,0x00,0x00,0x00 }; // Reader nonce
byte_t Auth[4] = { 0x00, t.sectors[e_sector].trailer, 0x00, 0x00 };
byte_t AuthEnc[4] = { 0x00, t.sectors[e_sector].trailer, 0x00, 0x00 };
byte_t AuthEncPar[8] = { 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 };
uint8_t Nr[4] = { 0x00,0x00,0x00,0x00 }; // Reader nonce
uint8_t Auth[4] = { 0x00, t.sectors[e_sector].trailer, 0x00, 0x00 };
uint8_t AuthEnc[4] = { 0x00, t.sectors[e_sector].trailer, 0x00, 0x00 };
uint8_t AuthEncPar[8] = { 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 };
byte_t ArEnc[8] = { 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 };
byte_t ArEncPar[8] = { 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 };
uint8_t ArEnc[8] = { 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 };
uint8_t ArEncPar[8] = { 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 };
byte_t Rx[MAX_FRAME_LEN]; // Tag response
byte_t RxPar[MAX_FRAME_LEN]; // Tag response
uint8_t Rx[MAX_FRAME_LEN]; // Tag response
uint8_t RxPar[MAX_FRAME_LEN]; // Tag response
size_t RxLen;
u_int32_t Nt, NtLast, NtProbe, NtEnc, Ks1;
@ -669,25 +673,25 @@ int mf_enhanced_auth(int e_sector, int a_sector, mftag t, mfreader r, denonce *d
// print_hex(Auth, 4);
// We need full control over the CRC
if (!nfc_configure(r.pdi, NDO_HANDLE_CRC, false)) {
nfc_perror (r.pdi, "nfc_configure crc");
if (nfc_device_set_property_bool(r.pdi, NP_HANDLE_CRC, false) < 0) {
nfc_perror (r.pdi, "nfc_device_set_property_bool crc");
exit (EXIT_FAILURE);
}
// Request plain tag-nonce
// TODO: Set NDO_EASY_FRAMING option only once if possible
if (!nfc_configure (r.pdi, NDO_EASY_FRAMING, false)) {
nfc_perror (r.pdi, "nfc_configure framing");
// TODO: Set NP_EASY_FRAMING option only once if possible
if (nfc_device_set_property_bool (r.pdi, NP_EASY_FRAMING, false) < 0) {
nfc_perror (r.pdi, "nfc_device_set_property_bool framing");
exit (EXIT_FAILURE);
}
if (!nfc_initiator_transceive_bytes(r.pdi, Auth, 4, Rx, &RxLen, NULL)) {
if (nfc_initiator_transceive_bytes(r.pdi, Auth, 4, Rx, &RxLen, 0) < 0) {
fprintf(stdout, "Error while requesting plain tag-nonce\n");
exit(EXIT_FAILURE);
}
if (!nfc_configure (r.pdi, NDO_EASY_FRAMING, true)) {
nfc_perror (r.pdi, "nfc_configure");
if (nfc_device_set_property_bool (r.pdi, NP_EASY_FRAMING, true) < 0) {
nfc_perror (r.pdi, "nfc_device_set_property_bool");
exit (EXIT_FAILURE);
}
// print_hex(Rx, 4);
@ -723,15 +727,15 @@ int mf_enhanced_auth(int e_sector, int a_sector, mftag t, mfreader r, denonce *d
}
// Finally we want to send arbitrary parity bits
if (!nfc_configure(r.pdi, NDO_HANDLE_PARITY, false)) {
nfc_perror (r.pdi, "nfc_configure parity");
if (nfc_device_set_property_bool(r.pdi, NP_HANDLE_PARITY, false) < 0) {
nfc_perror (r.pdi, "nfc_device_set_property_bool parity");
exit (EXIT_FAILURE);
}
// Transmit reader-answer
// fprintf(stdout, "\t{Ar}:\t");
// print_hex_par(ArEnc, 64, ArEncPar);
if ((!nfc_initiator_transceive_bits(r.pdi, ArEnc, 64, ArEncPar, Rx, &RxLen, RxPar)) || (RxLen != 32)) {
if (((RxLen = nfc_initiator_transceive_bits(r.pdi, ArEnc, 64, ArEncPar, Rx, RxPar)) < 0) || (RxLen != 32)) {
ERR ("Reader-answer transfer error, exiting..");
exit (EXIT_FAILURE);
}
@ -760,7 +764,7 @@ int mf_enhanced_auth(int e_sector, int a_sector, mftag t, mfreader r, denonce *d
}
// Sending the encrypted Auth command
if (!nfc_initiator_transceive_bits(r.pdi, AuthEnc, 32, AuthEncPar,Rx, &RxLen, RxPar)) {
if ((RxLen = nfc_initiator_transceive_bits(r.pdi, AuthEnc, 32, AuthEncPar,Rx, RxPar)) < 0) {
fprintf(stdout, "Error requesting encrypted tag-nonce\n");
exit (EXIT_FAILURE);
}
@ -788,8 +792,8 @@ int mf_enhanced_auth(int e_sector, int a_sector, mftag t, mfreader r, denonce *d
ArEnc[i] = crypto1_byte(pcs, 0x00, 0) ^ (Nt&0xFF);
ArEncPar[i] = filter(pcs->odd) ^ oddparity(Nt);
}
nfc_configure(r.pdi,NDO_HANDLE_PARITY,false);
if ((!nfc_initiator_transceive_bits(r.pdi, ArEnc, 64, ArEncPar, Rx, &RxLen, RxPar)) || (RxLen != 32)) {
nfc_device_set_property_bool(r.pdi,NP_HANDLE_PARITY,false);
if (((RxLen = nfc_initiator_transceive_bits(r.pdi, ArEnc, 64, ArEncPar, Rx, RxPar)) < 0) || (RxLen != 32)) {
ERR ("Reader-answer transfer error, exiting..");
exit (EXIT_FAILURE);
}
@ -818,19 +822,19 @@ int mf_enhanced_auth(int e_sector, int a_sector, mftag t, mfreader r, denonce *d
// Encrypt the parity bits with the 4 plaintext bytes
AuthEncPar[i] = filter(pcs->odd) ^ oddparity(Auth[i]);
}
if (!nfc_initiator_transceive_bits(r.pdi, AuthEnc, 32, AuthEncPar,Rx, &RxLen, RxPar)) {
if ((RxLen = nfc_initiator_transceive_bits(r.pdi, AuthEnc, 32, AuthEncPar,Rx, RxPar)) < 0) {
ERR ("while requesting encrypted tag-nonce");
exit (EXIT_FAILURE);
}
// Finally we want to send arbitrary parity bits
if (!nfc_configure(r.pdi, NDO_HANDLE_PARITY, true)) {
nfc_perror (r.pdi, "nfc_configure parity restore M");
if (nfc_device_set_property_bool(r.pdi, NP_HANDLE_PARITY, true) < 0) {
nfc_perror (r.pdi, "nfc_device_set_property_bool parity restore M");
exit (EXIT_FAILURE);
}
if (!nfc_configure(r.pdi, NDO_HANDLE_CRC, true)) {
nfc_perror (r.pdi, "nfc_configure crc restore M");
if (nfc_device_set_property_bool(r.pdi, NP_HANDLE_CRC, true) < 0) {
nfc_perror (r.pdi, "nfc_device_set_property_bool crc restore M");
exit (EXIT_FAILURE);
}
@ -944,20 +948,20 @@ countKeys * uniqsort(uint64_t * possibleKeys, uint32_t size) {
// Return 1 if the nonce is invalid else return 0
int valid_nonce(uint32_t Nt, uint32_t NtEnc, uint32_t Ks1, byte_t * parity) {
int valid_nonce(uint32_t Nt, uint32_t NtEnc, uint32_t Ks1, uint8_t * parity) {
return ((odd_parity((Nt >> 24) & 0xFF) == ((parity[0]) ^ odd_parity((NtEnc >> 24) & 0xFF) ^ BIT(Ks1,16))) & \
(odd_parity((Nt >> 16) & 0xFF) == ((parity[1]) ^ odd_parity((NtEnc >> 16) & 0xFF) ^ BIT(Ks1,8))) & \
(odd_parity((Nt >> 8) & 0xFF) == ((parity[2]) ^ odd_parity((NtEnc >> 8) & 0xFF) ^ BIT(Ks1,0)))) ? 1 : 0;
}
void num_to_bytes(uint64_t n, uint32_t len, byte_t* dest) {
void num_to_bytes(uint64_t n, uint32_t len, uint8_t* dest) {
while (len--) {
dest[len] = (byte_t) n;
dest[len] = (uint8_t) n;
n >>= 8;
}
}
long long unsigned int bytes_to_num(byte_t* src, uint32_t len) {
long long unsigned int bytes_to_num(uint8_t* src, uint32_t len) {
uint64_t num = 0;
while (len--)
{

View file

@ -24,11 +24,11 @@
#define odd_parity(i) (( (i) ^ (i)>>1 ^ (i)>>2 ^ (i)>>3 ^ (i)>>4 ^ (i)>>5 ^ (i)>>6 ^ (i)>>7 ^ 1) & 0x01)
typedef struct {
byte_t KeyA[6];
byte_t KeyB[6];
uint8_t KeyA[6];
uint8_t KeyB[6];
bool foundKeyA;
bool foundKeyB;
byte_t trailer; // Value of a trailer block
uint8_t trailer; // Value of a trailer block
} sector;
typedef struct {
@ -36,11 +36,11 @@ typedef struct {
u_int32_t median;
u_int32_t num_distances;
u_int32_t tolerance;
byte_t parity[3]; // used for 3 bits of parity information
uint8_t parity[3]; // used for 3 bits of parity information
} denonce; // Revealed information about nonce
typedef struct {
nfc_target_t nt;
nfc_target nt;
sector * sectors; // Allocate later, we do not know the number of sectors yet
sector e_sector; // Exploit sector
uint32_t num_sectors;
@ -60,7 +60,7 @@ typedef struct {
} bKeys;
typedef struct {
nfc_device_t *pdi;
nfc_device *pdi;
} mfreader;
typedef struct {
@ -71,16 +71,16 @@ typedef struct {
void usage(FILE * stream, int errno);
void mf_init(mftag *t, mfreader *r);
void mf_configure(nfc_device_t* pdi);
void mf_select_tag(nfc_device_t* pdi, nfc_target_t* pnt);
void mf_configure(nfc_device* pdi);
void mf_select_tag(nfc_device* pdi, nfc_target* pnt);
int trailer_block(uint32_t block);
int find_exploit_sector(mftag t);
void mf_anticollision(mftag t, mfreader r);
int mf_enhanced_auth(int e_sector, int a_sector, mftag t, mfreader r, denonce *d, pKeys *pk, char mode, bool dumpKeysA);
uint32_t median(denonce d);
int compar_int(const void * a, const void * b);
int valid_nonce(uint32_t Nt, uint32_t NtEnc, uint32_t Ks1, byte_t * parity);
int valid_nonce(uint32_t Nt, uint32_t NtEnc, uint32_t Ks1, uint8_t * parity);
int compar_special_int(const void * a, const void * b);
countKeys * uniqsort(uint64_t *possibleKeys, uint32_t size);
void num_to_bytes(uint64_t n, uint32_t len, byte_t* dest);
long long unsigned int bytes_to_num(byte_t* src, uint32_t len);
void num_to_bytes(uint64_t n, uint32_t len, uint8_t* dest);
long long unsigned int bytes_to_num(uint8_t* src, uint32_t len);

View file

@ -48,13 +48,13 @@
* 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)
nfc_initiator_mifare_cmd (nfc_device *pnd, const mifare_cmd mc, const uint8_t ui8Block, mifare_param *pmp)
{
byte_t abtRx[265];
uint8_t abtRx[265];
size_t szRx = sizeof(abtRx);
size_t szParamLen;
byte_t abtCmd[265];
bool bEasyFraming;
uint8_t abtCmd[265];
//bool bEasyFraming;
abtCmd[0] = mc; // The MIFARE Classic command
abtCmd[1] = ui8Block; // The block address (1K=0x00..0x39, 4K=0x00..0xff)
@ -69,19 +69,19 @@ nfc_initiator_mifare_cmd (nfc_device_t * pnd, const mifare_cmd mc, const uint8_t
// Authenticate command
case MC_AUTH_A:
case MC_AUTH_B:
szParamLen = sizeof (mifare_param_auth);
szParamLen = sizeof (struct mifare_param_auth);
break;
// Data command
case MC_WRITE:
szParamLen = sizeof (mifare_param_data);
szParamLen = sizeof (struct mifare_param_data);
break;
// Value command
case MC_DECREMENT:
case MC_INCREMENT:
case MC_TRANSFER:
szParamLen = sizeof (mifare_param_value);
szParamLen = sizeof (struct mifare_param_value);
break;
// Please fix your code, you never should reach this statement
@ -92,32 +92,34 @@ nfc_initiator_mifare_cmd (nfc_device_t * pnd, const mifare_cmd mc, const uint8_t
// When available, copy the parameter bytes
if (szParamLen)
memcpy (abtCmd + 2, (byte_t *) pmp, szParamLen);
memcpy (abtCmd + 2, (uint8_t *) pmp, szParamLen);
bEasyFraming = pnd->bEasyFraming;
if (!nfc_configure (pnd, NDO_EASY_FRAMING, true)) {
nfc_perror (pnd, "nfc_configure");
// FIXME: Save and restore bEasyFraming
// bEasyFraming = nfc_device_get_property_bool (pnd, NP_EASY_FRAMING, &bEasyFraming);
if (nfc_device_set_property_bool (pnd, NP_EASY_FRAMING, true) < 0) {
nfc_perror (pnd, "nfc_device_set_property_bool");
return false;
}
// Fire the mifare command
if (!nfc_initiator_transceive_bytes (pnd, abtCmd, 2 + szParamLen, abtRx, &szRx, NULL)) {
if (pnd->iLastError == EINVRXFRAM) {
// "Invalid received frame" AKA EINVRXFRAM, usual means we are
int res;
if ((res = nfc_initiator_transceive_bytes (pnd, abtCmd, 2 + szParamLen, abtRx, &szRx, -1)) < 0) {
if (res == NFC_ERFTRANS) {
// "Invalid received frame", usual means we are
// authenticated on a sector but the requested MIFARE cmd (read, write)
// is not permitted by current acces bytes;
// So there is nothing to do here.
} else if (pnd->iLastError == EMFAUTH) {
// In MFOC, we have to hide authentication errors :)
} else {
nfc_perror (pnd, "nfc_initiator_transceive_bytes");
}
nfc_configure (pnd, NDO_EASY_FRAMING, bEasyFraming);
// XXX nfc_device_set_property_bool (pnd, NP_EASY_FRAMING, bEasyFraming);
return false;
}
if (!nfc_configure (pnd, NDO_EASY_FRAMING, bEasyFraming)) {
nfc_perror (pnd, "nfc_configure");
/* XXX
if (nfc_device_set_property_bool (pnd, NP_EASY_FRAMING, bEasyFraming) < 0) {
nfc_perror (pnd, "nfc_device_set_property_bool");
return false;
}
*/
// When we have executed a read command, copy the received bytes into the param
if (mc == MC_READ) {

View file

@ -38,7 +38,7 @@
# include <nfc/nfc-types.h>
// Compiler directive, set struct alignment to 1 byte_t for compatibility
// Compiler directive, set struct alignment to 1 uint8_t for compatibility
# pragma pack(1)
typedef enum {
@ -53,50 +53,50 @@ typedef enum {
} mifare_cmd;
// MIFARE command params
typedef struct {
byte_t abtKey[6];
byte_t abtUid[4];
} mifare_param_auth;
struct mifare_param_auth {
uint8_t abtKey[6];
uint8_t abtUid[4];
};
typedef struct {
byte_t abtData[16];
} mifare_param_data;
struct mifare_param_data {
uint8_t abtData[16];
};
typedef struct {
byte_t abtValue[4];
} mifare_param_value;
struct mifare_param_value {
uint8_t abtValue[4];
};
typedef union {
mifare_param_auth mpa;
mifare_param_data mpd;
mifare_param_value mpv;
struct mifare_param_auth mpa;
struct mifare_param_data mpd;
struct mifare_param_value mpv;
} mifare_param;
// Reset struct alignment to default
# 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 *pnd, const mifare_cmd mc, const uint8_t ui8Block, mifare_param *pmp);
// Compiler directive, set struct alignment to 1 byte_t for compatibility
// Compiler directive, set struct alignment to 1 uint8_t for compatibility
# 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];
uint8_t abtUID[4];
uint8_t btBCC;
uint8_t btUnknown;
uint8_t abtATQA[2];
uint8_t abtUnknown[8];
} mifare_classic_block_manufacturer;
typedef struct {
byte_t abtData[16];
uint8_t abtData[16];
} mifare_classic_block_data;
typedef struct {
byte_t abtKeyA[6];
byte_t abtAccessBits[4];
byte_t abtKeyB[6];
uint8_t abtKeyA[6];
uint8_t abtAccessBits[4];
uint8_t abtKeyB[6];
} mifare_classic_block_trailer;
typedef union {
@ -111,17 +111,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];
uint8_t sn0[3];
uint8_t btBCC0;
uint8_t sn1[4];
uint8_t btBCC1;
uint8_t internal;
uint8_t lock[2];
uint8_t otp[4];
} mifareul_block_manufacturer;
typedef struct {
byte_t abtData[16];
uint8_t abtData[16];
} mifareul_block_data;
typedef union {

View file

@ -33,7 +33,7 @@
#include "nfc-utils.h"
static const byte_t OddParity[256] = {
static const uint8_t OddParity[256] = {
1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1,
0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0,
0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0,
@ -52,14 +52,14 @@ 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)
uint8_t
oddparity (const uint8_t bt)
{
return OddParity[bt];
}
void
oddparity_bytes_ts (const byte_t * pbtData, const size_t szLen, byte_t * pbtPar)
oddparity_bytes_ts (const uint8_t *pbtData, const size_t szLen, uint8_t *pbtPar)
{
size_t szByteNr;
// Calculate the parity bits for the command
@ -69,7 +69,7 @@ oddparity_bytes_ts (const byte_t * pbtData, const size_t szLen, byte_t * pbtPar)
}
void
print_hex (const byte_t * pbtData, const size_t szBytes)
print_hex (const uint8_t *pbtData, const size_t szBytes)
{
size_t szPos;
@ -80,7 +80,7 @@ print_hex (const byte_t * pbtData, const size_t szBytes)
}
void
print_hex_bits (const byte_t * pbtData, const size_t szBits)
print_hex_bits (const uint8_t *pbtData, const size_t szBits)
{
uint8_t uRemainder;
size_t szPos;
@ -102,7 +102,7 @@ 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)
print_hex_par (const uint8_t *pbtData, const size_t szBits, const uint8_t *pbtDataPar)
{
uint8_t uRemainder;
size_t szPos;
@ -133,7 +133,7 @@ print_hex_par (const byte_t * pbtData, const size_t szBits, const byte_t * pbtDa
#define SAK_ISO18092_COMPLIANT 0x40
void
print_nfc_iso14443a_info (const nfc_iso14443a_info_t nai, bool verbose)
print_nfc_iso14443a_info (const nfc_iso14443a_info nai, bool verbose)
{
printf (" ATQA (SENS_RES): ");
print_hex (nai.abtAtqa, 2);
@ -202,7 +202,7 @@ print_nfc_iso14443a_info (const nfc_iso14443a_info_t nai, bool verbose)
size_t offset = 1;
if (nai.abtAts[0] & 0x10) { // TA(1) present
byte_t TA = nai.abtAts[offset];
uint8_t TA = nai.abtAts[offset];
offset++;
printf ("* Bit Rate Capability:\n");
if (TA == 0) {
@ -234,7 +234,7 @@ print_nfc_iso14443a_info (const nfc_iso14443a_info_t nai, bool verbose)
}
}
if (nai.abtAts[0] & 0x20) { // TB(1) present
byte_t TB= nai.abtAts[offset];
uint8_t TB= nai.abtAts[offset];
offset++;
printf ("* Frame Waiting Time: %.4g ms\n",256.0*16.0*(1<<((TB & 0xf0) >> 4))/13560.0);
if ((TB & 0x0f) == 0) {
@ -244,7 +244,7 @@ print_nfc_iso14443a_info (const nfc_iso14443a_info_t nai, bool verbose)
}
}
if (nai.abtAts[0] & 0x40) { // TC(1) present
byte_t TC = nai.abtAts[offset];
uint8_t TC = nai.abtAts[offset];
offset++;
if (TC & 0x1) {
printf("* Node ADdress supported\n");
@ -260,20 +260,20 @@ print_nfc_iso14443a_info (const nfc_iso14443a_info_t nai, bool verbose)
if (nai.szAtsLen > offset) {
printf ("* Historical bytes Tk: " );
print_hex (nai.abtAts + offset, (nai.szAtsLen - offset));
byte_t CIB = nai.abtAts[offset];
uint8_t CIB = nai.abtAts[offset];
offset++;
if (CIB != 0x00 && CIB != 0x10 && (CIB & 0xf0) != 0x80) {
printf(" * Proprietary format\n");
if (CIB == 0xc1) {
printf(" * Tag byte: Mifare or virtual cards of various types\n");
byte_t L = nai.abtAts[offset];
uint8_t L = nai.abtAts[offset];
offset++;
if (L != (nai.szAtsLen - offset)) {
printf(" * Warning: Type Identification Coding length (%i)", L);
printf(" not matching Tk length (%zi)\n", (nai.szAtsLen - offset));
}
if ((nai.szAtsLen - offset - 2) > 0) { // Omit 2 CRC bytes
byte_t CTC = nai.abtAts[offset];
uint8_t CTC = nai.abtAts[offset];
offset++;
printf(" * Chip Type: ");
switch (CTC & 0xf0) {
@ -316,7 +316,7 @@ print_nfc_iso14443a_info (const nfc_iso14443a_info_t nai, bool verbose)
}
}
if ((nai.szAtsLen - offset) > 0) { // Omit 2 CRC bytes
byte_t CVC = nai.abtAts[offset];
uint8_t CVC = nai.abtAts[offset];
offset++;
printf(" * Chip Status: ");
switch (CVC & 0xf0) {
@ -350,7 +350,7 @@ print_nfc_iso14443a_info (const nfc_iso14443a_info_t nai, bool verbose)
}
}
if ((nai.szAtsLen - offset) > 0) { // Omit 2 CRC bytes
byte_t VCS = nai.abtAts[offset];
uint8_t VCS = nai.abtAts[offset];
offset++;
printf(" * Specifics (Virtual Card Selection):\n");
if ((VCS & 0x09) == 0x00) {
@ -530,17 +530,19 @@ print_nfc_iso14443a_info (const nfc_iso14443a_info_t nai, bool verbose)
}
void
print_nfc_felica_info (const nfc_felica_info_t nfi, bool verbose)
print_nfc_felica_info (const nfc_felica_info nfi, bool verbose)
{
(void) verbose;
printf (" ID (NFCID2): ");
print_hex (nfi.abtId, 8);
printf (" Parameter (PAD): ");
print_hex (nfi.abtPad, 8);
printf (" System Code (SC): ");
print_hex (nfi.abtSysCode, 2);
}
void
print_nfc_jewel_info (const nfc_jewel_info_t nji, bool verbose)
print_nfc_jewel_info (const nfc_jewel_info nji, bool verbose)
{
(void) verbose;
printf (" ATQA (SENS_RES): ");
@ -553,7 +555,7 @@ print_nfc_jewel_info (const nfc_jewel_info_t nji, bool verbose)
#define PI_NAD_SUPPORTED 0x01
#define PI_CID_SUPPORTED 0x02
void
print_nfc_iso14443b_info (const nfc_iso14443b_info_t nbi, bool verbose)
print_nfc_iso14443b_info (const nfc_iso14443b_info nbi, bool verbose)
{
const int iMaxFrameSizes[] = { 16, 24, 32, 40, 48, 64, 96, 128, 256 };
printf (" PUPI: ");
@ -608,7 +610,7 @@ print_nfc_iso14443b_info (const nfc_iso14443b_info_t nbi, bool verbose)
}
void
print_nfc_iso14443bi_info (const nfc_iso14443bi_info_t nii, bool verbose)
print_nfc_iso14443bi_info (const nfc_iso14443bi_info nii, bool verbose)
{
printf (" DIV: ");
print_hex (nii.abtDIV, 4);
@ -632,7 +634,7 @@ print_nfc_iso14443bi_info (const nfc_iso14443bi_info_t nii, bool verbose)
}
void
print_nfc_iso14443b2sr_info (const nfc_iso14443b2sr_info_t nsi, bool verbose)
print_nfc_iso14443b2sr_info (const nfc_iso14443b2sr_info nsi, bool verbose)
{
(void) verbose;
printf (" UID: ");
@ -640,7 +642,7 @@ print_nfc_iso14443b2sr_info (const nfc_iso14443b2sr_info_t nsi, bool verbose)
}
void
print_nfc_iso14443b2ct_info (const nfc_iso14443b2ct_info_t nci, bool verbose)
print_nfc_iso14443b2ct_info (const nfc_iso14443b2ct_info nci, bool verbose)
{
(void) verbose;
uint32_t uid;
@ -653,7 +655,7 @@ print_nfc_iso14443b2ct_info (const nfc_iso14443b2ct_info_t nci, bool verbose)
}
void
print_nfc_dep_info (const nfc_dep_info_t ndi, bool verbose)
print_nfc_dep_info (const nfc_dep_info ndi, bool verbose)
{
(void) verbose;
printf (" NFCID3: ");
@ -668,53 +670,8 @@ print_nfc_dep_info (const nfc_dep_info_t ndi, bool verbose)
}
}
/**
* @brief Tries to parse arguments to find device descriptions.
* @return Returns the list of found device descriptions.
*/
nfc_device_desc_t *
parse_args (int argc, const char *argv[], size_t * szFound, bool * verbose)
{
nfc_device_desc_t *pndd = 0;
int arg;
*szFound = 0;
// Get commandline options
for (arg = 1; arg < argc; arg++) {
if (0 == strcmp (argv[arg], "--device")) {
// FIXME: this device selection by command line options is terrible & does not support USB/PCSC drivers
if (argc > arg + 1) {
char buffer[256];
pndd = malloc (sizeof (nfc_device_desc_t));
strncpy (buffer, argv[++arg], 256);
// Driver.
pndd->pcDriver = (char *) malloc (256);
strcpy (pndd->pcDriver, strtok (buffer, ":"));
// Port.
strcpy (pndd->acPort, strtok (NULL, ":"));
// Speed.
sscanf (strtok (NULL, ":"), "%u", &pndd->uiSpeed);
*szFound = 1;
} else {
errx (1, "usage: %s [--device driver:port:speed]", argv[0]);
}
}
if ((0 == strcmp (argv[arg], "-v")) || (0 == strcmp (argv[arg], "--verbose"))) {
*verbose = true;
}
}
return pndd;
}
const char *
str_nfc_baud_rate (const nfc_baud_rate_t nbr)
str_nfc_baud_rate (const nfc_baud_rate nbr)
{
switch(nbr) {
case NBR_UNDEFINED:
@ -737,7 +694,7 @@ str_nfc_baud_rate (const nfc_baud_rate_t nbr)
}
void
print_nfc_target (const nfc_target_t nt, bool verbose)
print_nfc_target (const nfc_target nt, bool verbose)
{
switch(nt.nm.nmt) {
case NMT_ISO14443A:
@ -769,7 +726,7 @@ print_nfc_target (const nfc_target_t nt, bool verbose)
print_nfc_iso14443b2ct_info (nt.nti.nci, verbose);
break;
case NMT_DEP:
printf ("D.E.P. (%s) target:\n", str_nfc_baud_rate(nt.nm.nbr));
printf ("D.E.P. (%s, %s) target:\n", str_nfc_baud_rate(nt.nm.nbr), (nt.nti.ndi.ndm == NDM_ACTIVE)? "active mode" : "passive mode");
print_nfc_dep_info (nt.nti.ndi, verbose);
break;
}

View file

@ -79,21 +79,23 @@
# define ERR(...) warnx ("ERROR: " __VA_ARGS__ )
#endif
byte_t oddparity (const byte_t bt);
void oddparity_byte_ts (const byte_t * pbtData, const size_t szLen, byte_t * pbtPar);
uint8_t oddparity (const uint8_t bt);
void oddparity_uint8_ts (const uint8_t *pbtData, const size_t szLen, uint8_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 uint8_t *pbtData, const size_t szLen);
void print_hex_bits (const uint8_t *pbtData, const size_t szBits);
void print_hex_par (const uint8_t *pbtData, const size_t szBits, const uint8_t *pbtDataPar);
void print_nfc_iso14443a_info (const nfc_iso14443a_info_t nai, bool verbose);
void print_nfc_iso14443b_info (const nfc_iso14443b_info_t nbi, bool verbose);
void print_nfc_felica_info (const nfc_felica_info_t nfi, bool verbose);
void print_nfc_jewel_info (const nfc_jewel_info_t nji, bool verbose);
void print_nfc_dep_info (const nfc_dep_info_t ndi, bool verbose);
void print_nfc_iso14443a_info (const nfc_iso14443a_info nai, bool verbose);
void print_nfc_iso14443b_info (const nfc_iso14443b_info nbi, bool verbose);
void print_nfc_iso14443bi_info (const nfc_iso14443bi_info nii, bool verbose);
void print_nfc_iso14443b2sr_info (const nfc_iso14443b2sr_info nsi, bool verbose);
void print_nfc_iso14443b2ct_info (const nfc_iso14443b2ct_info nci, bool verbose);
void print_nfc_felica_info (const nfc_felica_info nfi, bool verbose);
void print_nfc_jewel_info (const nfc_jewel_info nji, bool verbose);
void print_nfc_dep_info (const nfc_dep_info ndi, bool verbose);
const char * str_nfc_baud_rate (const nfc_baud_rate nbr);
void print_nfc_target (const nfc_target_t nt, bool verbose);
nfc_device_desc_t *parse_args (int argc, const char *argv[], size_t * szFound, bool * verbose);
void print_nfc_target (const nfc_target nt, bool verbose);
#endif