Re-ident nfc-mfclassic.c using "indent -br -ce --line-length120 -nut -i2 -ppi 2 " command line.

This commit is contained in:
Romuald Conty 2010-06-07 09:05:35 +00:00
parent 8bac5355dd
commit 3c1a61349f

View file

@ -58,28 +58,39 @@ static byte_t keys[] = {
0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
}; };
static size_t num_keys = sizeof (keys) / 6; static size_t num_keys = sizeof (keys) / 6;
static void print_success_or_failure(bool bFailure, uint32_t* uiBlockCounter) static void
print_success_or_failure (bool bFailure, uint32_t * uiBlockCounter)
{ {
printf ("%c", (bFailure) ? 'x' : '.'); printf ("%c", (bFailure) ? 'x' : '.');
if (uiBlockCounter) if (uiBlockCounter)
*uiBlockCounter += (bFailure) ? 0 : 4; *uiBlockCounter += (bFailure) ? 0 : 4;
} }
static bool is_first_block(uint32_t uiBlock) static bool
is_first_block (uint32_t uiBlock)
{ {
// Test if we are in the small or big sectors // Test if we are in the small or big sectors
if (uiBlock < 128) return ((uiBlock)%4 == 0); else return ((uiBlock)%16 == 0); if (uiBlock < 128)
return ((uiBlock) % 4 == 0);
else
return ((uiBlock) % 16 == 0);
} }
static bool is_trailer_block(uint32_t uiBlock) static bool
is_trailer_block (uint32_t uiBlock)
{ {
// Test if we are in the small or big sectors // Test if we are in the small or big sectors
if (uiBlock < 128) return ((uiBlock+1)%4 == 0); else return ((uiBlock+1)%16 == 0); if (uiBlock < 128)
return ((uiBlock + 1) % 4 == 0);
else
return ((uiBlock + 1) % 16 == 0);
} }
static uint32_t get_trailer_block(uint32_t uiFirstBlock) static uint32_t
get_trailer_block (uint32_t uiFirstBlock)
{ {
// Test if we are in the small or big sectors // Test if we are in the small or big sectors
uint32_t trailer_block = 0; uint32_t trailer_block = 0;
@ -91,15 +102,15 @@ static uint32_t get_trailer_block(uint32_t uiFirstBlock)
return trailer_block; return trailer_block;
} }
static bool authenticate(uint32_t uiBlock) static bool
authenticate (uint32_t uiBlock)
{ {
mifare_cmd mc; mifare_cmd mc;
uint32_t uiTrailerBlock; uint32_t uiTrailerBlock;
size_t key_index; size_t key_index;
// Key file authentication. // Key file authentication.
if (bUseKeyFile) if (bUseKeyFile) {
{
// Set the authentication information (uid) // Set the authentication information (uid)
memcpy (mp.mpa.abtUid, nti.nai.abtUid, 4); memcpy (mp.mpa.abtUid, nti.nai.abtUid, 4);
@ -107,8 +118,7 @@ static bool authenticate(uint32_t uiBlock)
uiTrailerBlock = get_trailer_block (uiBlock); uiTrailerBlock = get_trailer_block (uiBlock);
// Determin if we should use the a or the b key // Determin if we should use the a or the b key
if (bUseKeyA) if (bUseKeyA) {
{
mc = MC_AUTH_A; mc = MC_AUTH_A;
memcpy (mp.mpa.abtKey, mtKeys.amb[uiTrailerBlock].mbt.abtKeyA, 6); memcpy (mp.mpa.abtKey, mtKeys.amb[uiTrailerBlock].mbt.abtKeyA, 6);
} else { } else {
@ -120,21 +130,17 @@ static bool authenticate(uint32_t uiBlock)
if (nfc_initiator_mifare_cmd (pnd, mc, uiBlock, &mp)) if (nfc_initiator_mifare_cmd (pnd, mc, uiBlock, &mp))
return true; return true;
} }
// Auto authentication. // Auto authentication.
else else {
{
// Determin if we should use the a or the b key // Determin if we should use the a or the b key
mc = (bUseKeyA) ? MC_AUTH_A : MC_AUTH_B; mc = (bUseKeyA) ? MC_AUTH_A : MC_AUTH_B;
// Set the authentication information (uid) // Set the authentication information (uid)
memcpy (mp.mpa.abtUid, nti.nai.abtUid, 4); memcpy (mp.mpa.abtUid, nti.nai.abtUid, 4);
for (key_index = 0; key_index < num_keys; key_index++) for (key_index = 0; key_index < num_keys; key_index++) {
{
memcpy (mp.mpa.abtKey, keys + (key_index * 6), 6); memcpy (mp.mpa.abtKey, keys + (key_index * 6), 6);
if (nfc_initiator_mifare_cmd(pnd, mc, uiBlock, &mp)) if (nfc_initiator_mifare_cmd (pnd, mc, uiBlock, &mp)) {
{
/** /**
* @note: what about the other key? * @note: what about the other key?
*/ */
@ -153,7 +159,8 @@ static bool authenticate(uint32_t uiBlock)
return false; return false;
} }
static bool read_card(void) static bool
read_card (void)
{ {
int32_t iBlock; int32_t iBlock;
bool bFailure = false; bool bFailure = false;
@ -162,21 +169,17 @@ static bool read_card(void)
printf ("Reading out %d blocks |", uiBlocks + 1); printf ("Reading out %d blocks |", uiBlocks + 1);
// Read the card from end to begin // Read the card from end to begin
for (iBlock=uiBlocks; iBlock>=0; iBlock--) for (iBlock = uiBlocks; iBlock >= 0; iBlock--) {
{
// Authenticate everytime we reach a trailer block // Authenticate everytime we reach a trailer block
if (is_trailer_block(iBlock)) if (is_trailer_block (iBlock)) {
{
// Skip this the first time, bFailure it means nothing (yet) // Skip this the first time, bFailure it means nothing (yet)
if (iBlock != uiBlocks) if (iBlock != uiBlocks)
print_success_or_failure (bFailure, &uiReadBlocks); print_success_or_failure (bFailure, &uiReadBlocks);
// Show if the readout went well // Show if the readout went well
if (bFailure) if (bFailure) {
{
// When a failure occured we need to redo the anti-collision // When a failure occured we need to redo the anti-collision
if (!nfc_initiator_select_tag(pnd,NM_ISO14443A_106,NULL,0,&nti)) if (!nfc_initiator_select_tag (pnd, NM_ISO14443A_106, NULL, 0, &nti)) {
{
printf ("!\nError: tag was removed\n"); printf ("!\nError: tag was removed\n");
return false; return false;
} }
@ -186,15 +189,12 @@ static bool read_card(void)
fflush (stdout); fflush (stdout);
// Try to authenticate for the current sector // Try to authenticate for the current sector
if (!authenticate(iBlock)) if (!authenticate (iBlock)) {
{
printf ("!\nError: authentication failed for block %02x\n", iBlock); printf ("!\nError: authentication failed for block %02x\n", iBlock);
return false; return false;
} }
// Try to read out the trailer // Try to read out the trailer
if (nfc_initiator_mifare_cmd(pnd,MC_READ,iBlock,&mp)) if (nfc_initiator_mifare_cmd (pnd, MC_READ, iBlock, &mp)) {
{
// Copy the keys over from our key dump and store the retrieved access bits // Copy the keys over from our key dump and store the retrieved access bits
memcpy (mtDump.amb[iBlock].mbt.abtKeyA, mtKeys.amb[iBlock].mbt.abtKeyA, 6); memcpy (mtDump.amb[iBlock].mbt.abtKeyA, mtKeys.amb[iBlock].mbt.abtKeyA, 6);
memcpy (mtDump.amb[iBlock].mbt.abtAccessBits, mp.mpd.abtData + 6, 4); memcpy (mtDump.amb[iBlock].mbt.abtAccessBits, mp.mpd.abtData + 6, 4);
@ -202,11 +202,9 @@ static bool read_card(void)
} }
} else { } else {
// Make sure a earlier readout did not fail // Make sure a earlier readout did not fail
if (!bFailure) if (!bFailure) {
{
// Try to read out the data block // Try to read out the data block
if (nfc_initiator_mifare_cmd(pnd,MC_READ,iBlock,&mp)) if (nfc_initiator_mifare_cmd (pnd, MC_READ, iBlock, &mp)) {
{
memcpy (mtDump.amb[iBlock].mbd.abtData, mp.mpd.abtData, 16); memcpy (mtDump.amb[iBlock].mbd.abtData, mp.mpd.abtData, 16);
} else { } else {
bFailure = true; bFailure = true;
@ -222,7 +220,8 @@ static bool read_card(void)
return true; return true;
} }
static bool write_card(void) static bool
write_card (void)
{ {
uint32_t uiBlock; uint32_t uiBlock;
bool bFailure = false; bool bFailure = false;
@ -231,21 +230,17 @@ static bool write_card(void)
printf ("Writing %d blocks |", uiBlocks + 1); printf ("Writing %d blocks |", uiBlocks + 1);
// Write the card from begin to end; // Write the card from begin to end;
for (uiBlock=0; uiBlock<=uiBlocks; uiBlock++) for (uiBlock = 0; uiBlock <= uiBlocks; uiBlock++) {
{
// Authenticate everytime we reach the first sector of a new block // Authenticate everytime we reach the first sector of a new block
if (is_first_block(uiBlock)) if (is_first_block (uiBlock)) {
{
// Skip this the first time, bFailure it means nothing (yet) // Skip this the first time, bFailure it means nothing (yet)
if (uiBlock != 0) if (uiBlock != 0)
print_success_or_failure (bFailure, &uiWriteBlocks); print_success_or_failure (bFailure, &uiWriteBlocks);
// Show if the readout went well // Show if the readout went well
if (bFailure) if (bFailure) {
{
// When a failure occured we need to redo the anti-collision // When a failure occured we need to redo the anti-collision
if (!nfc_initiator_select_tag(pnd,NM_ISO14443A_106,NULL,0,&nti)) if (!nfc_initiator_select_tag (pnd, NM_ISO14443A_106, NULL, 0, &nti)) {
{
printf ("!\nError: tag was removed\n"); printf ("!\nError: tag was removed\n");
return false; return false;
} }
@ -255,15 +250,13 @@ static bool write_card(void)
fflush (stdout); fflush (stdout);
// Try to authenticate for the current sector // Try to authenticate for the current sector
if (!authenticate(uiBlock)) if (!authenticate (uiBlock)) {
{
printf ("!\nError: authentication failed for block %02x\n", uiBlock); printf ("!\nError: authentication failed for block %02x\n", uiBlock);
return false; return false;
} }
} }
if (is_trailer_block(uiBlock)) if (is_trailer_block (uiBlock)) {
{
// Copy the keys over from our key dump and store the retrieved access bits // Copy the keys over from our key dump and store the retrieved access bits
memcpy (mp.mpd.abtData, mtDump.amb[uiBlock].mbt.abtKeyA, 6); memcpy (mp.mpd.abtData, mtDump.amb[uiBlock].mbt.abtKeyA, 6);
memcpy (mp.mpd.abtData + 6, mtDump.amb[uiBlock].mbt.abtAccessBits, 4); memcpy (mp.mpd.abtData + 6, mtDump.amb[uiBlock].mbt.abtAccessBits, 4);
@ -275,16 +268,16 @@ static bool write_card(void)
bFailure = true; bFailure = true;
} }
} else { } else {
// The first block 0x00 is read only, skip this // The first block 0x00 is read only, skip this
if (uiBlock == 0) continue; if (uiBlock == 0)
continue;
// Make sure a earlier write did not fail // Make sure a earlier write did not fail
if (!bFailure) if (!bFailure) {
{
// Try to write the data block // Try to write the data block
memcpy (mp.mpd.abtData, mtDump.amb[uiBlock].mbd.abtData, 16); memcpy (mp.mpd.abtData, mtDump.amb[uiBlock].mbd.abtData, 16);
if (!nfc_initiator_mifare_cmd(pnd,MC_WRITE,uiBlock,&mp)) bFailure = true; if (!nfc_initiator_mifare_cmd (pnd, MC_WRITE, uiBlock, &mp))
bFailure = true;
} }
} }
} }
@ -296,7 +289,8 @@ static bool write_card(void)
return true; return true;
} }
static void mifare_classic_extract_payload(const char* abDump, char* pbPayload) static void
mifare_classic_extract_payload (const char *abDump, char *pbPayload)
{ {
uint8_t uiSectorIndex; uint8_t uiSectorIndex;
uint8_t uiBlockIndex; uint8_t uiBlockIndex;
@ -313,14 +307,16 @@ static void mifare_classic_extract_payload(const char* abDump, char* pbPayload)
} }
} }
typedef enum { typedef enum
{
ACTION_READ, ACTION_READ,
ACTION_WRITE, ACTION_WRITE,
ACTION_EXTRACT, ACTION_EXTRACT,
ACTION_USAGE ACTION_USAGE
} action_t; } action_t;
static void print_usage(const char* pcProgramName) static void
print_usage (const char *pcProgramName)
{ {
printf ("Usage: "); printf ("Usage: ");
printf ("%s r|w a|b <dump.mfd> [<keys.mfd>]\n", pcProgramName); printf ("%s r|w a|b <dump.mfd> [<keys.mfd>]\n", pcProgramName);
@ -335,7 +331,8 @@ static void print_usage(const char* pcProgramName)
printf (" <payload.bin> - Binary file where payload will be extracted\n"); printf (" <payload.bin> - Binary file where payload will be extracted\n");
} }
int main(int argc, const char* argv[]) int
main (int argc, const char *argv[])
{ {
bool b4K; bool b4K;
action_t atAction = ACTION_USAGE; action_t atAction = ACTION_USAGE;
@ -344,24 +341,20 @@ int main(int argc, const char* argv[])
FILE *pfDump = NULL; FILE *pfDump = NULL;
const char *command = argv[1]; const char *command = argv[1];
if(argc < 2) if (argc < 2) {
{
print_usage (argv[0]); print_usage (argv[0]);
exit (EXIT_FAILURE); exit (EXIT_FAILURE);
} }
if(strcmp(command, "r") == 0) if (strcmp (command, "r") == 0) {
{
atAction = ACTION_READ; atAction = ACTION_READ;
bUseKeyA = tolower ((int) ((unsigned char) *(argv[2]))) == 'a'; bUseKeyA = tolower ((int) ((unsigned char) *(argv[2]))) == 'a';
bUseKeyFile = (argc > 4); bUseKeyFile = (argc > 4);
} else if(strcmp(command, "w") == 0) } else if (strcmp (command, "w") == 0) {
{
atAction = ACTION_WRITE; atAction = ACTION_WRITE;
bUseKeyA = tolower ((int) ((unsigned char) *(argv[2]))) == 'a'; bUseKeyA = tolower ((int) ((unsigned char) *(argv[2]))) == 'a';
bUseKeyFile = (argc > 4); bUseKeyFile = (argc > 4);
} else if(strcmp(command, "x") == 0) } else if (strcmp (command, "x") == 0) {
{
atAction = ACTION_EXTRACT; atAction = ACTION_EXTRACT;
} }
@ -372,22 +365,18 @@ int main(int argc, const char* argv[])
break; break;
case ACTION_READ: case ACTION_READ:
case ACTION_WRITE: case ACTION_WRITE:
if (argc < 4) if (argc < 4) {
{
print_usage (argv[0]); print_usage (argv[0]);
exit (EXIT_FAILURE); exit (EXIT_FAILURE);
} }
if (bUseKeyFile) if (bUseKeyFile) {
{
pfKeys = fopen (argv[4], "rb"); pfKeys = fopen (argv[4], "rb");
if (pfKeys == NULL) if (pfKeys == NULL) {
{
printf ("Could not open keys file: %s\n", argv[4]); printf ("Could not open keys file: %s\n", argv[4]);
exit (EXIT_FAILURE); exit (EXIT_FAILURE);
} }
if (fread(&mtKeys,1,sizeof(mtKeys),pfKeys) != sizeof(mtKeys)) if (fread (&mtKeys, 1, sizeof (mtKeys), pfKeys) != sizeof (mtKeys)) {
{
printf ("Could not read keys file: %s\n", argv[4]); printf ("Could not read keys file: %s\n", argv[4]);
fclose (pfKeys); fclose (pfKeys);
exit (EXIT_FAILURE); exit (EXIT_FAILURE);
@ -400,14 +389,12 @@ int main(int argc, const char* argv[])
} else { } else {
pfDump = fopen (argv[3], "rb"); pfDump = fopen (argv[3], "rb");
if (pfDump == NULL) if (pfDump == NULL) {
{
printf ("Could not open dump file: %s\n", argv[3]); printf ("Could not open dump file: %s\n", argv[3]);
exit (EXIT_FAILURE); exit (EXIT_FAILURE);
} }
if (fread(&mtDump,1,sizeof(mtDump),pfDump) != sizeof(mtDump)) if (fread (&mtDump, 1, sizeof (mtDump), pfDump) != sizeof (mtDump)) {
{
printf ("Could not read dump file: %s\n", argv[3]); printf ("Could not read dump file: %s\n", argv[3]);
fclose (pfDump); fclose (pfDump);
exit (EXIT_FAILURE); exit (EXIT_FAILURE);
@ -418,8 +405,7 @@ int main(int argc, const char* argv[])
// Try to open the NFC reader // Try to open the NFC reader
pnd = nfc_connect (NULL); pnd = nfc_connect (NULL);
if (pnd == NULL) if (pnd == NULL) {
{
printf ("Error connecting NFC reader\n"); printf ("Error connecting NFC reader\n");
exit (EXIT_FAILURE); exit (EXIT_FAILURE);
} }
@ -440,50 +426,43 @@ int main(int argc, const char* argv[])
printf ("Connected to NFC reader: %s\n", pnd->acName); printf ("Connected to NFC reader: %s\n", pnd->acName);
// Try to find a MIFARE Classic tag // Try to find a MIFARE Classic tag
if (!nfc_initiator_select_tag(pnd,NM_ISO14443A_106,NULL,0,&nti)) if (!nfc_initiator_select_tag (pnd, NM_ISO14443A_106, NULL, 0, &nti)) {
{
printf ("Error: no tag was found\n"); printf ("Error: no tag was found\n");
nfc_disconnect (pnd); nfc_disconnect (pnd);
exit (EXIT_FAILURE); exit (EXIT_FAILURE);
} }
// Test if we are dealing with a MIFARE compatible tag // Test if we are dealing with a MIFARE compatible tag
if ((nti.nai.btSak & 0x08) == 0) if ((nti.nai.btSak & 0x08) == 0) {
{
printf ("Error: tag is not a MIFARE Classic card\n"); printf ("Error: tag is not a MIFARE Classic card\n");
nfc_disconnect (pnd); nfc_disconnect (pnd);
exit (EXIT_FAILURE); exit (EXIT_FAILURE);
} }
if (bUseKeyFile) if (bUseKeyFile) {
{
// Get the info from the key dump // Get the info from the key dump
b4K = (mtKeys.amb[0].mbm.abtATQA[1] == 0x02); b4K = (mtKeys.amb[0].mbm.abtATQA[1] == 0x02);
pbtUID = mtKeys.amb[0].mbm.abtUID; pbtUID = mtKeys.amb[0].mbm.abtUID;
// Compare if key dump UID is the same as the current tag UID // Compare if key dump UID is the same as the current tag UID
if (memcmp(nti.nai.abtUid,pbtUID,4) != 0) if (memcmp (nti.nai.abtUid, pbtUID, 4) != 0) {
{ printf ("Expected MIFARE Classic %cK card with UID: %02x%02x%02x%02x\n", b4K ? '4' : '1', pbtUID[3], pbtUID[2],
printf("Expected MIFARE Classic %cK card with UID: %02x%02x%02x%02x\n",b4K?'4':'1', pbtUID[3], pbtUID[2], pbtUID[1], pbtUID[0]); pbtUID[1], pbtUID[0]);
} }
} }
// Get the info from the current tag // Get the info from the current tag
pbtUID = nti.nai.abtUid; pbtUID = nti.nai.abtUid;
b4K = (nti.nai.abtAtqa[1] == 0x02); b4K = (nti.nai.abtAtqa[1] == 0x02);
printf("Found MIFARE Classic %cK card with UID: %02x%02x%02x%02x\n",b4K?'4':'1', pbtUID[3], pbtUID[2], pbtUID[1], pbtUID[0]); printf ("Found MIFARE Classic %cK card with UID: %02x%02x%02x%02x\n", b4K ? '4' : '1', pbtUID[3], pbtUID[2],
pbtUID[1], pbtUID[0]);
uiBlocks = (b4K) ? 0xff : 0x3f; uiBlocks = (b4K) ? 0xff : 0x3f;
if (atAction == ACTION_READ) if (atAction == ACTION_READ) {
{ if (read_card ()) {
if (read_card())
{
printf ("Writing data to file: %s ... ", argv[3]); printf ("Writing data to file: %s ... ", argv[3]);
fflush (stdout); fflush (stdout);
pfDump = fopen (argv[3], "wb"); pfDump = fopen (argv[3], "wb");
if (fwrite(&mtDump,1,sizeof(mtDump),pfDump) != sizeof(mtDump)) if (fwrite (&mtDump, 1, sizeof (mtDump), pfDump) != sizeof (mtDump)) {
{
printf ("\nCould not write to file: %s\n", argv[3]); printf ("\nCould not write to file: %s\n", argv[3]);
exit (EXIT_FAILURE); exit (EXIT_FAILURE);
} }
@ -509,14 +488,12 @@ int main(int argc, const char* argv[])
pfDump = fopen (pcDump, "rb"); pfDump = fopen (pcDump, "rb");
if (pfDump == NULL) if (pfDump == NULL) {
{
printf ("Could not open dump file: %s\n", pcDump); printf ("Could not open dump file: %s\n", pcDump);
exit (EXIT_FAILURE); exit (EXIT_FAILURE);
} }
if (fread(abDump,1,sizeof(abDump),pfDump) != sizeof(abDump)) if (fread (abDump, 1, sizeof (abDump), pfDump) != sizeof (abDump)) {
{
printf ("Could not read dump file: %s\n", pcDump); printf ("Could not read dump file: %s\n", pcDump);
fclose (pfDump); fclose (pfDump);
exit (EXIT_FAILURE); exit (EXIT_FAILURE);
@ -527,8 +504,7 @@ int main(int argc, const char* argv[])
printf ("Writing data to file: %s\n", pcPayload); printf ("Writing data to file: %s\n", pcPayload);
pfPayload = fopen (pcPayload, "wb"); pfPayload = fopen (pcPayload, "wb");
if (fwrite(abPayload,1,sizeof(abPayload),pfPayload) != sizeof(abPayload)) if (fwrite (abPayload, 1, sizeof (abPayload), pfPayload) != sizeof (abPayload)) {
{
printf ("Could not write to file: %s\n", pcPayload); printf ("Could not write to file: %s\n", pcPayload);
exit (EXIT_FAILURE); exit (EXIT_FAILURE);
} }
@ -539,4 +515,3 @@ int main(int argc, const char* argv[])
exit (EXIT_SUCCESS); exit (EXIT_SUCCESS);
} }