update to use libnfc's trunk
This commit is contained in:
parent
ea0c8a7047
commit
5c62782645
6 changed files with 179 additions and 214 deletions
138
src/mfoc.c
138
src/mfoc.c
|
@ -50,7 +50,7 @@
|
||||||
#include "mfoc.h"
|
#include "mfoc.h"
|
||||||
|
|
||||||
int main(int argc, char * const argv[]) {
|
int main(int argc, char * const argv[]) {
|
||||||
const nfc_modulation_t nm = {
|
const nfc_modulation nm = {
|
||||||
.nmt = NMT_ISO14443A,
|
.nmt = NMT_ISO14443A,
|
||||||
.nbr = NBR_106,
|
.nbr = NBR_106,
|
||||||
};
|
};
|
||||||
|
@ -70,11 +70,11 @@ int main(int argc, char * const argv[]) {
|
||||||
bool skip = false;
|
bool skip = false;
|
||||||
|
|
||||||
// Next default key specified as option (-k)
|
// Next default key specified as option (-k)
|
||||||
byte_t * defKeys = NULL, *p;
|
uint8_t * defKeys = NULL, *p;
|
||||||
size_t defKeys_len = 0;
|
size_t defKeys_len = 0;
|
||||||
|
|
||||||
// Array with default Mifare Classic keys
|
// 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)
|
{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
|
{0xa0, 0xa1, 0xa2, 0xa3, 0xa4, 0xa5}, // NFCForum MAD key
|
||||||
{0xd3, 0xf7, 0xd3, 0xf7, 0xd3, 0xf7}, // NFCForum content 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
|
// Initialize reader/tag structures
|
||||||
mf_init(&t, &r);
|
mf_init(&t, &r);
|
||||||
|
|
||||||
if (!nfc_initiator_init (r.pdi)) {
|
if (nfc_initiator_init (r.pdi) < 0) {
|
||||||
nfc_perror (r.pdi, "nfc_initiator_init");
|
nfc_perror (r.pdi, "nfc_initiator_init");
|
||||||
goto error;
|
goto error;
|
||||||
}
|
}
|
||||||
// Drop the field for a while, so can be reset
|
// Drop the field for a while, so can be reset
|
||||||
if (!nfc_configure(r.pdi, NDO_ACTIVATE_FIELD, true)) {
|
if (nfc_device_set_property_bool(r.pdi, NP_ACTIVATE_FIELD, true) < 0) {
|
||||||
nfc_perror (r.pdi, "nfc_configure activate field");
|
nfc_perror (r.pdi, "nfc_device_set_property_bool activate field");
|
||||||
goto error;
|
goto error;
|
||||||
}
|
}
|
||||||
// Let the reader only try once to find a tag
|
// Let the reader only try once to find a tag
|
||||||
if (!nfc_configure(r.pdi, NDO_INFINITE_SELECT, false)) {
|
if (nfc_device_set_property_bool(r.pdi, NP_INFINITE_SELECT, false) < 0) {
|
||||||
nfc_perror (r.pdi, "nfc_configure infinite select");
|
nfc_perror (r.pdi, "nfc_device_set_property_bool infinite select");
|
||||||
goto error;
|
goto error;
|
||||||
}
|
}
|
||||||
// Configure the CRC and Parity settings
|
// Configure the CRC and Parity settings
|
||||||
if (!nfc_configure(r.pdi, NDO_HANDLE_CRC, true)) {
|
if (nfc_device_set_property_bool(r.pdi, NP_HANDLE_CRC, true) < 0) {
|
||||||
nfc_perror (r.pdi, "nfc_configure crc");
|
nfc_perror (r.pdi, "nfc_device_set_property_bool crc");
|
||||||
goto error;
|
goto error;
|
||||||
}
|
}
|
||||||
if (!nfc_configure(r.pdi, NDO_HANDLE_PARITY, true)) {
|
if (nfc_device_set_property_bool(r.pdi, NP_HANDLE_PARITY, true) < 0) {
|
||||||
nfc_perror (r.pdi, "nfc_configure parity");
|
nfc_perror (r.pdi, "nfc_device_set_property_bool parity");
|
||||||
goto error;
|
goto error;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -196,7 +196,7 @@ int main(int argc, char * const argv[]) {
|
||||||
*/
|
*/
|
||||||
|
|
||||||
// mf_select_tag(r.pdi, &(t.nt));
|
// 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");
|
nfc_perror (r.pdi, "nfc_initiator_select_passive_target");
|
||||||
goto error;
|
goto error;
|
||||||
}
|
}
|
||||||
|
@ -507,14 +507,16 @@ int main(int argc, char * const argv[]) {
|
||||||
free(d.distances);
|
free(d.distances);
|
||||||
|
|
||||||
// Reset the "advanced" configuration to normal
|
// Reset the "advanced" configuration to normal
|
||||||
nfc_configure(r.pdi, NDO_HANDLE_CRC, true);
|
nfc_device_set_property_bool(r.pdi, NP_HANDLE_CRC, true);
|
||||||
nfc_configure(r.pdi, NDO_HANDLE_PARITY, true);
|
nfc_device_set_property_bool(r.pdi, NP_HANDLE_PARITY, true);
|
||||||
|
|
||||||
// Disconnect device and exit
|
// Disconnect device and exit
|
||||||
nfc_disconnect(r.pdi);
|
nfc_close(r.pdi);
|
||||||
|
nfc_exit(NULL);
|
||||||
exit (EXIT_SUCCESS);
|
exit (EXIT_SUCCESS);
|
||||||
error:
|
error:
|
||||||
nfc_disconnect(r.pdi);
|
nfc_close(r.pdi);
|
||||||
|
nfc_exit(NULL);
|
||||||
exit (EXIT_FAILURE);
|
exit (EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -542,53 +544,55 @@ void usage(FILE * stream, int errno) {
|
||||||
|
|
||||||
void mf_init(mftag *t, mfreader *r) {
|
void mf_init(mftag *t, mfreader *r) {
|
||||||
// Connect to the first NFC device
|
// Connect to the first NFC device
|
||||||
r->pdi = nfc_connect(NULL);
|
nfc_init(NULL);
|
||||||
|
r->pdi = nfc_open(NULL, NULL);
|
||||||
if (!r->pdi) {
|
if (!r->pdi) {
|
||||||
printf ("No NFC device found.\n");
|
printf ("No NFC device found.\n");
|
||||||
exit (EXIT_FAILURE);
|
exit (EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void mf_configure(nfc_device_t* pdi) {
|
void mf_configure(nfc_device* pdi) {
|
||||||
if (!nfc_initiator_init (pdi)) {
|
if (nfc_initiator_init (pdi) < 0) {
|
||||||
nfc_perror (pdi, "nfc_initiator_init");
|
nfc_perror (pdi, "nfc_initiator_init");
|
||||||
exit (EXIT_FAILURE);
|
exit (EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
// Drop the field for a while, so can be reset
|
// Drop the field for a while, so can be reset
|
||||||
if (!nfc_configure(pdi, NDO_ACTIVATE_FIELD, false)) {
|
if (nfc_device_set_property_bool(pdi, NP_ACTIVATE_FIELD, false) < 0) {
|
||||||
nfc_perror (pdi, "nfc_configure activate field");
|
nfc_perror (pdi, "nfc_device_set_property_bool activate field");
|
||||||
exit (EXIT_FAILURE);
|
exit (EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
// Let the reader only try once to find a tag
|
// Let the reader only try once to find a tag
|
||||||
if (!nfc_configure(pdi, NDO_INFINITE_SELECT, false)) {
|
if (nfc_device_set_property_bool(pdi, NP_INFINITE_SELECT, false) < 0) {
|
||||||
nfc_perror (pdi, "nfc_configure infinite select");
|
nfc_perror (pdi, "nfc_device_set_property_bool infinite select");
|
||||||
exit (EXIT_FAILURE);
|
exit (EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
// Configure the CRC and Parity settings
|
// Configure the CRC and Parity settings
|
||||||
if (!nfc_configure(pdi, NDO_HANDLE_CRC, true)) {
|
if (nfc_device_set_property_bool(pdi, NP_HANDLE_CRC, true) < 0) {
|
||||||
nfc_perror (pdi, "nfc_configure crc");
|
nfc_perror (pdi, "nfc_device_set_property_bool crc");
|
||||||
exit (EXIT_FAILURE);
|
exit (EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
if (!nfc_configure(pdi, NDO_HANDLE_PARITY, true)) {
|
if (nfc_device_set_property_bool(pdi, NP_HANDLE_PARITY, true) < 0) {
|
||||||
nfc_perror (pdi, "nfc_configure parity");
|
nfc_perror (pdi, "nfc_device_set_property_bool parity");
|
||||||
exit (EXIT_FAILURE);
|
exit (EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
// Enable the field so more power consuming cards can power themselves up
|
// Enable the field so more power consuming cards can power themselves up
|
||||||
if (!nfc_configure(pdi, NDO_ACTIVATE_FIELD, true)) {
|
if (nfc_device_set_property_bool(pdi, NP_ACTIVATE_FIELD, true) < 0) {
|
||||||
nfc_perror (pdi, "nfc_configure activate field");
|
nfc_perror (pdi, "nfc_device_set_property_bool activate field");
|
||||||
exit (EXIT_FAILURE);
|
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
|
// Poll for a ISO14443A (MIFARE) tag
|
||||||
const nfc_modulation_t nm = {
|
const nfc_modulation nm = {
|
||||||
.nmt = NMT_ISO14443A,
|
.nmt = NMT_ISO14443A,
|
||||||
.nbr = NBR_106,
|
.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");
|
ERR ("Unable to connect to the MIFARE Classic tag");
|
||||||
nfc_disconnect(pdi);
|
nfc_close(pdi);
|
||||||
|
nfc_exit(NULL);
|
||||||
exit (EXIT_FAILURE);
|
exit (EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -625,11 +629,11 @@ int find_exploit_sector(mftag t) {
|
||||||
}
|
}
|
||||||
|
|
||||||
void mf_anticollision(mftag t, mfreader r) {
|
void mf_anticollision(mftag t, mfreader r) {
|
||||||
const nfc_modulation_t nm = {
|
const nfc_modulation nm = {
|
||||||
.nmt = NMT_ISO14443A,
|
.nmt = NMT_ISO14443A,
|
||||||
.nbr = NBR_106,
|
.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");
|
nfc_perror (r.pdi, "nfc_initiator_select_passive_target");
|
||||||
ERR ("Tag has been removed");
|
ERR ("Tag has been removed");
|
||||||
exit (EXIT_FAILURE);
|
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"
|
// Possible key counter, just continue with a previous "session"
|
||||||
uint32_t kcount = pk->size;
|
uint32_t kcount = pk->size;
|
||||||
|
|
||||||
byte_t Nr[4] = { 0x00,0x00,0x00,0x00 }; // Reader nonce
|
uint8_t Nr[4] = { 0x00,0x00,0x00,0x00 }; // Reader nonce
|
||||||
byte_t Auth[4] = { 0x00, t.sectors[e_sector].trailer, 0x00, 0x00 };
|
uint8_t Auth[4] = { 0x00, t.sectors[e_sector].trailer, 0x00, 0x00 };
|
||||||
byte_t AuthEnc[4] = { 0x00, t.sectors[e_sector].trailer, 0x00, 0x00 };
|
uint8_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 AuthEncPar[8] = { 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 };
|
||||||
|
|
||||||
byte_t ArEnc[8] = { 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 };
|
uint8_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 ArEncPar[8] = { 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 };
|
||||||
|
|
||||||
byte_t Rx[MAX_FRAME_LEN]; // Tag response
|
uint8_t Rx[MAX_FRAME_LEN]; // Tag response
|
||||||
byte_t RxPar[MAX_FRAME_LEN]; // Tag response
|
uint8_t RxPar[MAX_FRAME_LEN]; // Tag response
|
||||||
size_t RxLen;
|
size_t RxLen;
|
||||||
|
|
||||||
u_int32_t Nt, NtLast, NtProbe, NtEnc, Ks1;
|
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);
|
// print_hex(Auth, 4);
|
||||||
|
|
||||||
// We need full control over the CRC
|
// We need full control over the CRC
|
||||||
if (!nfc_configure(r.pdi, NDO_HANDLE_CRC, false)) {
|
if (nfc_device_set_property_bool(r.pdi, NP_HANDLE_CRC, false) < 0) {
|
||||||
nfc_perror (r.pdi, "nfc_configure crc");
|
nfc_perror (r.pdi, "nfc_device_set_property_bool crc");
|
||||||
exit (EXIT_FAILURE);
|
exit (EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Request plain tag-nonce
|
// Request plain tag-nonce
|
||||||
// TODO: Set NDO_EASY_FRAMING option only once if possible
|
// TODO: Set NP_EASY_FRAMING option only once if possible
|
||||||
if (!nfc_configure (r.pdi, NDO_EASY_FRAMING, false)) {
|
if (nfc_device_set_property_bool (r.pdi, NP_EASY_FRAMING, false) < 0) {
|
||||||
nfc_perror (r.pdi, "nfc_configure framing");
|
nfc_perror (r.pdi, "nfc_device_set_property_bool framing");
|
||||||
exit (EXIT_FAILURE);
|
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");
|
fprintf(stdout, "Error while requesting plain tag-nonce\n");
|
||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!nfc_configure (r.pdi, NDO_EASY_FRAMING, true)) {
|
if (nfc_device_set_property_bool (r.pdi, NP_EASY_FRAMING, true) < 0) {
|
||||||
nfc_perror (r.pdi, "nfc_configure");
|
nfc_perror (r.pdi, "nfc_device_set_property_bool");
|
||||||
exit (EXIT_FAILURE);
|
exit (EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
// print_hex(Rx, 4);
|
// 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
|
// Finally we want to send arbitrary parity bits
|
||||||
if (!nfc_configure(r.pdi, NDO_HANDLE_PARITY, false)) {
|
if (nfc_device_set_property_bool(r.pdi, NP_HANDLE_PARITY, false) < 0) {
|
||||||
nfc_perror (r.pdi, "nfc_configure parity");
|
nfc_perror (r.pdi, "nfc_device_set_property_bool parity");
|
||||||
exit (EXIT_FAILURE);
|
exit (EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Transmit reader-answer
|
// Transmit reader-answer
|
||||||
// fprintf(stdout, "\t{Ar}:\t");
|
// fprintf(stdout, "\t{Ar}:\t");
|
||||||
// print_hex_par(ArEnc, 64, ArEncPar);
|
// 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..");
|
ERR ("Reader-answer transfer error, exiting..");
|
||||||
exit (EXIT_FAILURE);
|
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
|
// 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");
|
fprintf(stdout, "Error requesting encrypted tag-nonce\n");
|
||||||
exit (EXIT_FAILURE);
|
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);
|
ArEnc[i] = crypto1_byte(pcs, 0x00, 0) ^ (Nt&0xFF);
|
||||||
ArEncPar[i] = filter(pcs->odd) ^ oddparity(Nt);
|
ArEncPar[i] = filter(pcs->odd) ^ oddparity(Nt);
|
||||||
}
|
}
|
||||||
nfc_configure(r.pdi,NDO_HANDLE_PARITY,false);
|
nfc_device_set_property_bool(r.pdi,NP_HANDLE_PARITY,false);
|
||||||
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..");
|
ERR ("Reader-answer transfer error, exiting..");
|
||||||
exit (EXIT_FAILURE);
|
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
|
// Encrypt the parity bits with the 4 plaintext bytes
|
||||||
AuthEncPar[i] = filter(pcs->odd) ^ oddparity(Auth[i]);
|
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");
|
ERR ("while requesting encrypted tag-nonce");
|
||||||
exit (EXIT_FAILURE);
|
exit (EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Finally we want to send arbitrary parity bits
|
// Finally we want to send arbitrary parity bits
|
||||||
if (!nfc_configure(r.pdi, NDO_HANDLE_PARITY, true)) {
|
if (nfc_device_set_property_bool(r.pdi, NP_HANDLE_PARITY, true) < 0) {
|
||||||
nfc_perror (r.pdi, "nfc_configure parity restore M");
|
nfc_perror (r.pdi, "nfc_device_set_property_bool parity restore M");
|
||||||
exit (EXIT_FAILURE);
|
exit (EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!nfc_configure(r.pdi, NDO_HANDLE_CRC, true)) {
|
if (nfc_device_set_property_bool(r.pdi, NP_HANDLE_CRC, true) < 0) {
|
||||||
nfc_perror (r.pdi, "nfc_configure crc restore M");
|
nfc_perror (r.pdi, "nfc_device_set_property_bool crc restore M");
|
||||||
exit (EXIT_FAILURE);
|
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
|
// 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))) & \
|
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 >> 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;
|
(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--) {
|
while (len--) {
|
||||||
dest[len] = (byte_t) n;
|
dest[len] = (uint8_t) n;
|
||||||
n >>= 8;
|
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;
|
uint64_t num = 0;
|
||||||
while (len--)
|
while (len--)
|
||||||
{
|
{
|
||||||
|
|
22
src/mfoc.h
22
src/mfoc.h
|
@ -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)
|
#define odd_parity(i) (( (i) ^ (i)>>1 ^ (i)>>2 ^ (i)>>3 ^ (i)>>4 ^ (i)>>5 ^ (i)>>6 ^ (i)>>7 ^ 1) & 0x01)
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
byte_t KeyA[6];
|
uint8_t KeyA[6];
|
||||||
byte_t KeyB[6];
|
uint8_t KeyB[6];
|
||||||
bool foundKeyA;
|
bool foundKeyA;
|
||||||
bool foundKeyB;
|
bool foundKeyB;
|
||||||
byte_t trailer; // Value of a trailer block
|
uint8_t trailer; // Value of a trailer block
|
||||||
} sector;
|
} sector;
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
|
@ -36,11 +36,11 @@ typedef struct {
|
||||||
u_int32_t median;
|
u_int32_t median;
|
||||||
u_int32_t num_distances;
|
u_int32_t num_distances;
|
||||||
u_int32_t tolerance;
|
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
|
} denonce; // Revealed information about nonce
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
nfc_target_t nt;
|
nfc_target nt;
|
||||||
sector * sectors; // Allocate later, we do not know the number of sectors yet
|
sector * sectors; // Allocate later, we do not know the number of sectors yet
|
||||||
sector e_sector; // Exploit sector
|
sector e_sector; // Exploit sector
|
||||||
uint32_t num_sectors;
|
uint32_t num_sectors;
|
||||||
|
@ -60,7 +60,7 @@ typedef struct {
|
||||||
} bKeys;
|
} bKeys;
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
nfc_device_t *pdi;
|
nfc_device *pdi;
|
||||||
} mfreader;
|
} mfreader;
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
|
@ -71,16 +71,16 @@ typedef struct {
|
||||||
|
|
||||||
void usage(FILE * stream, int errno);
|
void usage(FILE * stream, int errno);
|
||||||
void mf_init(mftag *t, mfreader *r);
|
void mf_init(mftag *t, mfreader *r);
|
||||||
void mf_configure(nfc_device_t* pdi);
|
void mf_configure(nfc_device* pdi);
|
||||||
void mf_select_tag(nfc_device_t* pdi, nfc_target_t* pnt);
|
void mf_select_tag(nfc_device* pdi, nfc_target* pnt);
|
||||||
int trailer_block(uint32_t block);
|
int trailer_block(uint32_t block);
|
||||||
int find_exploit_sector(mftag t);
|
int find_exploit_sector(mftag t);
|
||||||
void mf_anticollision(mftag t, mfreader r);
|
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);
|
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);
|
uint32_t median(denonce d);
|
||||||
int compar_int(const void * a, const void * b);
|
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);
|
int compar_special_int(const void * a, const void * b);
|
||||||
countKeys * uniqsort(uint64_t *possibleKeys, uint32_t size);
|
countKeys * uniqsort(uint64_t *possibleKeys, uint32_t size);
|
||||||
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);
|
||||||
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);
|
||||||
|
|
40
src/mifare.c
40
src/mifare.c
|
@ -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.
|
* The MIFARE Classic Specification (http://www.nxp.com/acrobat/other/identification/M001053_MF1ICS50_rev5_3.pdf) explains more about this process.
|
||||||
*/
|
*/
|
||||||
bool
|
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 szRx = sizeof(abtRx);
|
||||||
size_t szParamLen;
|
size_t szParamLen;
|
||||||
byte_t abtCmd[265];
|
uint8_t abtCmd[265];
|
||||||
bool bEasyFraming;
|
//bool bEasyFraming;
|
||||||
|
|
||||||
abtCmd[0] = mc; // The MIFARE Classic command
|
abtCmd[0] = mc; // The MIFARE Classic command
|
||||||
abtCmd[1] = ui8Block; // The block address (1K=0x00..0x39, 4K=0x00..0xff)
|
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
|
// Authenticate command
|
||||||
case MC_AUTH_A:
|
case MC_AUTH_A:
|
||||||
case MC_AUTH_B:
|
case MC_AUTH_B:
|
||||||
szParamLen = sizeof (mifare_param_auth);
|
szParamLen = sizeof (struct mifare_param_auth);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
// Data command
|
// Data command
|
||||||
case MC_WRITE:
|
case MC_WRITE:
|
||||||
szParamLen = sizeof (mifare_param_data);
|
szParamLen = sizeof (struct mifare_param_data);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
// Value command
|
// Value command
|
||||||
case MC_DECREMENT:
|
case MC_DECREMENT:
|
||||||
case MC_INCREMENT:
|
case MC_INCREMENT:
|
||||||
case MC_TRANSFER:
|
case MC_TRANSFER:
|
||||||
szParamLen = sizeof (mifare_param_value);
|
szParamLen = sizeof (struct mifare_param_value);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
// Please fix your code, you never should reach this statement
|
// 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
|
// When available, copy the parameter bytes
|
||||||
if (szParamLen)
|
if (szParamLen)
|
||||||
memcpy (abtCmd + 2, (byte_t *) pmp, szParamLen);
|
memcpy (abtCmd + 2, (uint8_t *) pmp, szParamLen);
|
||||||
|
|
||||||
bEasyFraming = pnd->bEasyFraming;
|
// FIXME: Save and restore bEasyFraming
|
||||||
if (!nfc_configure (pnd, NDO_EASY_FRAMING, true)) {
|
// bEasyFraming = nfc_device_get_property_bool (pnd, NP_EASY_FRAMING, &bEasyFraming);
|
||||||
nfc_perror (pnd, "nfc_configure");
|
if (nfc_device_set_property_bool (pnd, NP_EASY_FRAMING, true) < 0) {
|
||||||
|
nfc_perror (pnd, "nfc_device_set_property_bool");
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
// Fire the mifare command
|
// Fire the mifare command
|
||||||
if (!nfc_initiator_transceive_bytes (pnd, abtCmd, 2 + szParamLen, abtRx, &szRx, NULL)) {
|
int res;
|
||||||
if (pnd->iLastError == EINVRXFRAM) {
|
if ((res = nfc_initiator_transceive_bytes (pnd, abtCmd, 2 + szParamLen, abtRx, &szRx, -1)) < 0) {
|
||||||
// "Invalid received frame" AKA EINVRXFRAM, usual means we are
|
if (res == NFC_ERFTRANS) {
|
||||||
|
// "Invalid received frame", usual means we are
|
||||||
// authenticated on a sector but the requested MIFARE cmd (read, write)
|
// authenticated on a sector but the requested MIFARE cmd (read, write)
|
||||||
// is not permitted by current acces bytes;
|
// is not permitted by current acces bytes;
|
||||||
// So there is nothing to do here.
|
// So there is nothing to do here.
|
||||||
} else if (pnd->iLastError == EMFAUTH) {
|
|
||||||
// In MFOC, we have to hide authentication errors :)
|
|
||||||
} else {
|
} else {
|
||||||
nfc_perror (pnd, "nfc_initiator_transceive_bytes");
|
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;
|
return false;
|
||||||
}
|
}
|
||||||
if (!nfc_configure (pnd, NDO_EASY_FRAMING, bEasyFraming)) {
|
/* XXX
|
||||||
nfc_perror (pnd, "nfc_configure");
|
if (nfc_device_set_property_bool (pnd, NP_EASY_FRAMING, bEasyFraming) < 0) {
|
||||||
|
nfc_perror (pnd, "nfc_device_set_property_bool");
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
*/
|
||||||
|
|
||||||
// When we have executed a read command, copy the received bytes into the param
|
// When we have executed a read command, copy the received bytes into the param
|
||||||
if (mc == MC_READ) {
|
if (mc == MC_READ) {
|
||||||
|
|
66
src/mifare.h
66
src/mifare.h
|
@ -38,7 +38,7 @@
|
||||||
|
|
||||||
# include <nfc/nfc-types.h>
|
# 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)
|
# pragma pack(1)
|
||||||
|
|
||||||
typedef enum {
|
typedef enum {
|
||||||
|
@ -53,50 +53,50 @@ typedef enum {
|
||||||
} mifare_cmd;
|
} mifare_cmd;
|
||||||
|
|
||||||
// MIFARE command params
|
// MIFARE command params
|
||||||
typedef struct {
|
struct mifare_param_auth {
|
||||||
byte_t abtKey[6];
|
uint8_t abtKey[6];
|
||||||
byte_t abtUid[4];
|
uint8_t abtUid[4];
|
||||||
} mifare_param_auth;
|
};
|
||||||
|
|
||||||
typedef struct {
|
struct mifare_param_data {
|
||||||
byte_t abtData[16];
|
uint8_t abtData[16];
|
||||||
} mifare_param_data;
|
};
|
||||||
|
|
||||||
typedef struct {
|
struct mifare_param_value {
|
||||||
byte_t abtValue[4];
|
uint8_t abtValue[4];
|
||||||
} mifare_param_value;
|
};
|
||||||
|
|
||||||
typedef union {
|
typedef union {
|
||||||
mifare_param_auth mpa;
|
struct mifare_param_auth mpa;
|
||||||
mifare_param_data mpd;
|
struct mifare_param_data mpd;
|
||||||
mifare_param_value mpv;
|
struct mifare_param_value mpv;
|
||||||
} mifare_param;
|
} mifare_param;
|
||||||
|
|
||||||
// Reset struct alignment to default
|
// 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 *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)
|
# pragma pack(1)
|
||||||
|
|
||||||
// MIFARE Classic
|
// MIFARE Classic
|
||||||
typedef struct {
|
typedef struct {
|
||||||
byte_t abtUID[4];
|
uint8_t abtUID[4];
|
||||||
byte_t btBCC;
|
uint8_t btBCC;
|
||||||
byte_t btUnknown;
|
uint8_t btUnknown;
|
||||||
byte_t abtATQA[2];
|
uint8_t abtATQA[2];
|
||||||
byte_t abtUnknown[8];
|
uint8_t abtUnknown[8];
|
||||||
} mifare_classic_block_manufacturer;
|
} mifare_classic_block_manufacturer;
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
byte_t abtData[16];
|
uint8_t abtData[16];
|
||||||
} mifare_classic_block_data;
|
} mifare_classic_block_data;
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
byte_t abtKeyA[6];
|
uint8_t abtKeyA[6];
|
||||||
byte_t abtAccessBits[4];
|
uint8_t abtAccessBits[4];
|
||||||
byte_t abtKeyB[6];
|
uint8_t abtKeyB[6];
|
||||||
} mifare_classic_block_trailer;
|
} mifare_classic_block_trailer;
|
||||||
|
|
||||||
typedef union {
|
typedef union {
|
||||||
|
@ -111,17 +111,17 @@ typedef struct {
|
||||||
|
|
||||||
// MIFARE Ultralight
|
// MIFARE Ultralight
|
||||||
typedef struct {
|
typedef struct {
|
||||||
byte_t sn0[3];
|
uint8_t sn0[3];
|
||||||
byte_t btBCC0;
|
uint8_t btBCC0;
|
||||||
byte_t sn1[4];
|
uint8_t sn1[4];
|
||||||
byte_t btBCC1;
|
uint8_t btBCC1;
|
||||||
byte_t internal;
|
uint8_t internal;
|
||||||
byte_t lock[2];
|
uint8_t lock[2];
|
||||||
byte_t otp[4];
|
uint8_t otp[4];
|
||||||
} mifareul_block_manufacturer;
|
} mifareul_block_manufacturer;
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
byte_t abtData[16];
|
uint8_t abtData[16];
|
||||||
} mifareul_block_data;
|
} mifareul_block_data;
|
||||||
|
|
||||||
typedef union {
|
typedef union {
|
||||||
|
|
|
@ -33,7 +33,7 @@
|
||||||
|
|
||||||
#include "nfc-utils.h"
|
#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,
|
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,
|
||||||
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
|
1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1
|
||||||
};
|
};
|
||||||
|
|
||||||
byte_t
|
uint8_t
|
||||||
oddparity (const byte_t bt)
|
oddparity (const uint8_t bt)
|
||||||
{
|
{
|
||||||
return OddParity[bt];
|
return OddParity[bt];
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
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;
|
size_t szByteNr;
|
||||||
// Calculate the parity bits for the command
|
// 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
|
void
|
||||||
print_hex (const byte_t * pbtData, const size_t szBytes)
|
print_hex (const uint8_t *pbtData, const size_t szBytes)
|
||||||
{
|
{
|
||||||
size_t szPos;
|
size_t szPos;
|
||||||
|
|
||||||
|
@ -80,7 +80,7 @@ print_hex (const byte_t * pbtData, const size_t szBytes)
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
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;
|
uint8_t uRemainder;
|
||||||
size_t szPos;
|
size_t szPos;
|
||||||
|
@ -102,7 +102,7 @@ print_hex_bits (const byte_t * pbtData, const size_t szBits)
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
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;
|
uint8_t uRemainder;
|
||||||
size_t szPos;
|
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
|
#define SAK_ISO18092_COMPLIANT 0x40
|
||||||
|
|
||||||
void
|
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): ");
|
printf (" ATQA (SENS_RES): ");
|
||||||
print_hex (nai.abtAtqa, 2);
|
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;
|
size_t offset = 1;
|
||||||
if (nai.abtAts[0] & 0x10) { // TA(1) present
|
if (nai.abtAts[0] & 0x10) { // TA(1) present
|
||||||
byte_t TA = nai.abtAts[offset];
|
uint8_t TA = nai.abtAts[offset];
|
||||||
offset++;
|
offset++;
|
||||||
printf ("* Bit Rate Capability:\n");
|
printf ("* Bit Rate Capability:\n");
|
||||||
if (TA == 0) {
|
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
|
if (nai.abtAts[0] & 0x20) { // TB(1) present
|
||||||
byte_t TB= nai.abtAts[offset];
|
uint8_t TB= nai.abtAts[offset];
|
||||||
offset++;
|
offset++;
|
||||||
printf ("* Frame Waiting Time: %.4g ms\n",256.0*16.0*(1<<((TB & 0xf0) >> 4))/13560.0);
|
printf ("* Frame Waiting Time: %.4g ms\n",256.0*16.0*(1<<((TB & 0xf0) >> 4))/13560.0);
|
||||||
if ((TB & 0x0f) == 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
|
if (nai.abtAts[0] & 0x40) { // TC(1) present
|
||||||
byte_t TC = nai.abtAts[offset];
|
uint8_t TC = nai.abtAts[offset];
|
||||||
offset++;
|
offset++;
|
||||||
if (TC & 0x1) {
|
if (TC & 0x1) {
|
||||||
printf("* Node ADdress supported\n");
|
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) {
|
if (nai.szAtsLen > offset) {
|
||||||
printf ("* Historical bytes Tk: " );
|
printf ("* Historical bytes Tk: " );
|
||||||
print_hex (nai.abtAts + offset, (nai.szAtsLen - offset));
|
print_hex (nai.abtAts + offset, (nai.szAtsLen - offset));
|
||||||
byte_t CIB = nai.abtAts[offset];
|
uint8_t CIB = nai.abtAts[offset];
|
||||||
offset++;
|
offset++;
|
||||||
if (CIB != 0x00 && CIB != 0x10 && (CIB & 0xf0) != 0x80) {
|
if (CIB != 0x00 && CIB != 0x10 && (CIB & 0xf0) != 0x80) {
|
||||||
printf(" * Proprietary format\n");
|
printf(" * Proprietary format\n");
|
||||||
if (CIB == 0xc1) {
|
if (CIB == 0xc1) {
|
||||||
printf(" * Tag byte: Mifare or virtual cards of various types\n");
|
printf(" * Tag byte: Mifare or virtual cards of various types\n");
|
||||||
byte_t L = nai.abtAts[offset];
|
uint8_t L = nai.abtAts[offset];
|
||||||
offset++;
|
offset++;
|
||||||
if (L != (nai.szAtsLen - offset)) {
|
if (L != (nai.szAtsLen - offset)) {
|
||||||
printf(" * Warning: Type Identification Coding length (%i)", L);
|
printf(" * Warning: Type Identification Coding length (%i)", L);
|
||||||
printf(" not matching Tk length (%zi)\n", (nai.szAtsLen - offset));
|
printf(" not matching Tk length (%zi)\n", (nai.szAtsLen - offset));
|
||||||
}
|
}
|
||||||
if ((nai.szAtsLen - offset - 2) > 0) { // Omit 2 CRC bytes
|
if ((nai.szAtsLen - offset - 2) > 0) { // Omit 2 CRC bytes
|
||||||
byte_t CTC = nai.abtAts[offset];
|
uint8_t CTC = nai.abtAts[offset];
|
||||||
offset++;
|
offset++;
|
||||||
printf(" * Chip Type: ");
|
printf(" * Chip Type: ");
|
||||||
switch (CTC & 0xf0) {
|
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
|
if ((nai.szAtsLen - offset) > 0) { // Omit 2 CRC bytes
|
||||||
byte_t CVC = nai.abtAts[offset];
|
uint8_t CVC = nai.abtAts[offset];
|
||||||
offset++;
|
offset++;
|
||||||
printf(" * Chip Status: ");
|
printf(" * Chip Status: ");
|
||||||
switch (CVC & 0xf0) {
|
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
|
if ((nai.szAtsLen - offset) > 0) { // Omit 2 CRC bytes
|
||||||
byte_t VCS = nai.abtAts[offset];
|
uint8_t VCS = nai.abtAts[offset];
|
||||||
offset++;
|
offset++;
|
||||||
printf(" * Specifics (Virtual Card Selection):\n");
|
printf(" * Specifics (Virtual Card Selection):\n");
|
||||||
if ((VCS & 0x09) == 0x00) {
|
if ((VCS & 0x09) == 0x00) {
|
||||||
|
@ -530,17 +530,19 @@ print_nfc_iso14443a_info (const nfc_iso14443a_info_t nai, bool verbose)
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
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;
|
(void) verbose;
|
||||||
printf (" ID (NFCID2): ");
|
printf (" ID (NFCID2): ");
|
||||||
print_hex (nfi.abtId, 8);
|
print_hex (nfi.abtId, 8);
|
||||||
printf (" Parameter (PAD): ");
|
printf (" Parameter (PAD): ");
|
||||||
print_hex (nfi.abtPad, 8);
|
print_hex (nfi.abtPad, 8);
|
||||||
|
printf (" System Code (SC): ");
|
||||||
|
print_hex (nfi.abtSysCode, 2);
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
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;
|
(void) verbose;
|
||||||
printf (" ATQA (SENS_RES): ");
|
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_NAD_SUPPORTED 0x01
|
||||||
#define PI_CID_SUPPORTED 0x02
|
#define PI_CID_SUPPORTED 0x02
|
||||||
void
|
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 };
|
const int iMaxFrameSizes[] = { 16, 24, 32, 40, 48, 64, 96, 128, 256 };
|
||||||
printf (" PUPI: ");
|
printf (" PUPI: ");
|
||||||
|
@ -608,7 +610,7 @@ print_nfc_iso14443b_info (const nfc_iso14443b_info_t nbi, bool verbose)
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
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: ");
|
printf (" DIV: ");
|
||||||
print_hex (nii.abtDIV, 4);
|
print_hex (nii.abtDIV, 4);
|
||||||
|
@ -632,7 +634,7 @@ print_nfc_iso14443bi_info (const nfc_iso14443bi_info_t nii, bool verbose)
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
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;
|
(void) verbose;
|
||||||
printf (" UID: ");
|
printf (" UID: ");
|
||||||
|
@ -640,7 +642,7 @@ print_nfc_iso14443b2sr_info (const nfc_iso14443b2sr_info_t nsi, bool verbose)
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
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;
|
(void) verbose;
|
||||||
uint32_t uid;
|
uint32_t uid;
|
||||||
|
@ -653,7 +655,7 @@ print_nfc_iso14443b2ct_info (const nfc_iso14443b2ct_info_t nci, bool verbose)
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
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;
|
(void) verbose;
|
||||||
printf (" NFCID3: ");
|
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 *
|
const char *
|
||||||
str_nfc_baud_rate (const nfc_baud_rate_t nbr)
|
str_nfc_baud_rate (const nfc_baud_rate nbr)
|
||||||
{
|
{
|
||||||
switch(nbr) {
|
switch(nbr) {
|
||||||
case NBR_UNDEFINED:
|
case NBR_UNDEFINED:
|
||||||
|
@ -737,7 +694,7 @@ str_nfc_baud_rate (const nfc_baud_rate_t nbr)
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
print_nfc_target (const nfc_target_t nt, bool verbose)
|
print_nfc_target (const nfc_target nt, bool verbose)
|
||||||
{
|
{
|
||||||
switch(nt.nm.nmt) {
|
switch(nt.nm.nmt) {
|
||||||
case NMT_ISO14443A:
|
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);
|
print_nfc_iso14443b2ct_info (nt.nti.nci, verbose);
|
||||||
break;
|
break;
|
||||||
case NMT_DEP:
|
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);
|
print_nfc_dep_info (nt.nti.ndi, verbose);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
|
@ -79,21 +79,23 @@
|
||||||
# define ERR(...) warnx ("ERROR: " __VA_ARGS__ )
|
# define ERR(...) warnx ("ERROR: " __VA_ARGS__ )
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
byte_t oddparity (const byte_t bt);
|
uint8_t oddparity (const uint8_t bt);
|
||||||
void oddparity_byte_ts (const byte_t * pbtData, const size_t szLen, byte_t * pbtPar);
|
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 (const uint8_t *pbtData, const size_t szLen);
|
||||||
void print_hex_bits (const byte_t * pbtData, const size_t szBits);
|
void print_hex_bits (const uint8_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_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_iso14443a_info (const nfc_iso14443a_info nai, bool verbose);
|
||||||
void print_nfc_iso14443b_info (const nfc_iso14443b_info_t nbi, bool verbose);
|
void print_nfc_iso14443b_info (const nfc_iso14443b_info nbi, bool verbose);
|
||||||
void print_nfc_felica_info (const nfc_felica_info_t nfi, bool verbose);
|
void print_nfc_iso14443bi_info (const nfc_iso14443bi_info nii, bool verbose);
|
||||||
void print_nfc_jewel_info (const nfc_jewel_info_t nji, bool verbose);
|
void print_nfc_iso14443b2sr_info (const nfc_iso14443b2sr_info nsi, bool verbose);
|
||||||
void print_nfc_dep_info (const nfc_dep_info_t ndi, 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);
|
void print_nfc_target (const nfc_target nt, bool verbose);
|
||||||
|
|
||||||
nfc_device_desc_t *parse_args (int argc, const char *argv[], size_t * szFound, bool * verbose);
|
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
Loading…
Reference in a new issue