--- mfoc-0.10.2/src/mfoc.c	2011-05-18 11:18:29.000000000 +0200
+++ mfoc-0.10.2_more_keys/src/mfoc.c	2011-10-19 01:33:00.000000000 +0200
@@ -74,7 +74,6 @@
 	
 	// Array with default Mifare Classic keys
 	byte_t defaultKeys[][6] = {
-		{0xff, 0xff, 0xff, 0xff, 0xff, 0xff}, // User defined key slot
 		{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
@@ -90,6 +89,11 @@
 		{0x8f, 0xd0, 0xa4, 0xf2, 0x56, 0xe9}
 
 	};
+
+	int allKeysIndex = 0;
+	int allKeysCount = sizeof(defaultKeys) / sizeof(defaultKeys[0]);
+	byte_t (* allKeys)[6] = malloc(allKeysCount * sizeof(defaultKeys[0]));
+	memcpy(allKeys, defaultKeys, allKeysCount * sizeof(defaultKeys[0]));
 	
 	mftag		t;
 	mfreader	r;
@@ -135,7 +139,25 @@
 				} else {
 					bzero(defKey, 6);
 					num_to_bytes(strtoll(optarg, NULL, 16), 6, defKey);
-					memcpy(defaultKeys[0], defKey, 6);
+					
+					if (allKeysIndex == 0) {
+						byte_t (* allKeysNew)[6];
+						allKeysCount *= 2;
+						if ((allKeysNew = malloc(allKeysCount * sizeof(allKeys[0]))) == NULL) {
+							ERR ("Cannot allocate memory for defKey");
+							exit (EXIT_FAILURE);
+						} 
+
+						memcpy(allKeysNew[allKeysCount / 2], allKeys, allKeysCount / 2 * sizeof(allKeys[0]));
+						allKeysIndex = allKeysCount / 2;
+						
+						free(allKeys);
+						allKeys = allKeysNew;
+					}
+		
+					allKeysIndex--;
+					memcpy(allKeys[allKeysIndex], defKey, 6);		
+					
 				}
 				fprintf(stdout, "The custom key 0x%012llx has been added to the default keys\n", bytes_to_num(defKey, 6));
 				
@@ -244,56 +266,66 @@
 	for (i = 0; i < (t.num_sectors); ++i) {
 		t.sectors[i].foundKeyA = t.sectors[i].foundKeyB = false;
 	}
-	
+
 	print_nfc_iso14443a_info (t.nt.nti.nai, true);
 	
 	// Try to authenticate to all sectors with default keys
 	// Set the authentication information (uid)
 	memcpy(mp.mpa.abtUid, t.nt.nti.nai.abtUid, sizeof(mp.mpa.abtUid));
-	// Iterate over all keys (n = number of keys)
-	n = sizeof(defaultKeys)/sizeof(defaultKeys[0]);
-	for (key = 0; key < n; key++) {
-		if (key == 0 && defKey == NULL) ++key; // Custom key not provided, try another key
-		memcpy(mp.mpa.abtKey, defaultKeys[key], sizeof(mp.mpa.abtKey));
+	// Iterate over all keys (allKeysCount = number of keys)
+
+	for (key = allKeysIndex; key < allKeysCount; key++) {
+		// fuck that when we've found all keys
+		bool all_done = true;
+		memcpy(mp.mpa.abtKey, allKeys[key], sizeof(mp.mpa.abtKey));
 		fprintf(stdout, "[Key: %012llx] -> ", bytes_to_num(mp.mpa.abtKey, 6));
 		fprintf(stdout, "[");
 		i = 0; // Sector counter
 		// Iterate over every block, where we haven't found a key yet
 		for (block = 0; block <= t.num_blocks; ++block) {
 			if (trailer_block(block)) {
-				if (!t.sectors[i].foundKeyA) {
-					mc = MC_AUTH_A;
-					if (!nfc_initiator_mifare_cmd(r.pdi,mc,block,&mp)) {
-						// fprintf(stdout, "!!Error: AUTH [Key A:%012llx] sector %02x t_block %02x\n", 
-						// 	bytes_to_num(mp.mpa.abtKey, 6), i, block);
-						mf_anticollision(t, r);
-					} else {
-						// Save all information about successfull keyA authentization
-						memcpy(t.sectors[i].KeyA, mp.mpa.abtKey, sizeof(mp.mpa.abtKey));
-						t.sectors[i].foundKeyA = true;
+				if (!(t.sectors[i].foundKeyA && t.sectors[i].foundKeyB)) { 	
+					if (!t.sectors[i].foundKeyA) {
+						mc = MC_AUTH_A;
+						if (!nfc_initiator_mifare_cmd(r.pdi,mc,block,&mp)) {
+							// fprintf(stdout, "!!Error: AUTH [Key A:%012llx] sector %02x t_block %02x\n", 
+							// 	bytes_to_num(mp.mpa.abtKey, 6), i, block);
+							mf_anticollision(t, r);
+						} else {
+							// Save all information about successfull keyA authentization
+							memcpy(t.sectors[i].KeyA, mp.mpa.abtKey, sizeof(mp.mpa.abtKey));
+							t.sectors[i].foundKeyA = true;
+						}
 					}
-				}
-				if (!t.sectors[i].foundKeyB) {
-					mc = MC_AUTH_B;
-					if (!nfc_initiator_mifare_cmd(r.pdi,mc,block,&mp)) {
-						// fprintf(stdout, "!!Error: AUTH [Key B:%012llx] sector %02x t_block %02x\n", 
-						//	bytes_to_num(mp.mpa.abtKey, 6), i, block);
-						mf_anticollision(t, r);
-						// No success, try next block
-						t.sectors[i].trailer = block;
-					} else {
-						memcpy(t.sectors[i].KeyB, mp.mpa.abtKey, sizeof(mp.mpa.abtKey));
-						t.sectors[i].foundKeyB = true;
+					if (!t.sectors[i].foundKeyB) {
+						mc = MC_AUTH_B;
+						if (!nfc_initiator_mifare_cmd(r.pdi,mc,block,&mp)) {
+							// fprintf(stdout, "!!Error: AUTH [Key B:%012llx] sector %02x t_block %02x\n", 
+							//	bytes_to_num(mp.mpa.abtKey, 6), i, block);
+							mf_anticollision(t, r);
+							// No success, try next block
+							t.sectors[i].trailer = block;
+						} else {
+							memcpy(t.sectors[i].KeyB, mp.mpa.abtKey, sizeof(mp.mpa.abtKey));
+							t.sectors[i].foundKeyB = true;
+						}
 					}
-				}
-				if ((t.sectors[i].foundKeyA) || (t.sectors[i].foundKeyB)) {
-					fprintf(stdout, "x");
+					all_done &= (t.sectors[i].foundKeyA & t.sectors[i].foundKeyB);
+                                	mf_configure(r.pdi);
+                                	mf_anticollision(t, r);
+				}
+				if ((t.sectors[i].foundKeyA) && (t.sectors[i].foundKeyB)) {
+					fprintf(stdout, "X");
+				} else if (t.sectors[i].foundKeyA) {
+					fprintf(stdout, "a");
+				} else if (t.sectors[i].foundKeyB) {
+					fprintf(stdout, "b");
 				} else {
 					fprintf(stdout, ".");
 				}
+				
+
 				fflush(stdout);
-				mf_configure(r.pdi);
-				mf_anticollision(t, r);
 				// fprintf(stdout, "\nSuccess: AUTH [Key %c:%012llx] sector %02x t_block %02x\n", 
 				// 	(mc == MC_AUTH_A ? 'A' :'B'), bytes_to_num(mp.mpa.abtKey, 6), i, block);
 				// Save position of a trailer block to sector struct
@@ -301,6 +333,10 @@
 			}
 		}
 		fprintf(stdout, "]\n");
+		if (all_done) {
+			fprintf(stdout, "got all keys now, bye-bye!\n");
+			break;
+		}
 	}
 
 	fprintf(stdout, "\n");

