diff --git a/bin/Debug/desfire_example_c++.exe b/bin/Debug/desfire_example_c++.exe index 5307da94bf9785863d891778f3d33a5e7a33eefa..ed0d23055fce3261de12b8dfb26ffdc6317a6b36 100644 Binary files a/bin/Debug/desfire_example_c++.exe and b/bin/Debug/desfire_example_c++.exe differ diff --git a/bin/Release/config.txt b/bin/Release/config.txt index e47b19a154cde7b0942787e488f6ff87ccd7cb90..a24d7279f70f7570f025332fb5cc5eae1983b882 100644 --- a/bin/Release/config.txt +++ b/bin/Release/config.txt @@ -1,5 +1,5 @@ AES key: 00000000000000000000000000000000 -AID 3 bytes hex: 000000 -AID key number for auth: 1 -File ID: 1 +AID 3 bytes hex: A10000 +AID key number for auth: 0 +File ID: 2 Internal key number: 100 diff --git a/bin/Release/desfire_example_c++.exe b/bin/Release/desfire_example_c++.exe index 2681c76c0e54d9784cc30faba2f7e700730f4a56..f646d734942496e580a0b7d6e96e2d10b0f5e1a3 100644 Binary files a/bin/Release/desfire_example_c++.exe and b/bin/Release/desfire_example_c++.exe differ diff --git a/bin/Release/write.txt b/bin/Release/write.txt index 36941c83c1624c2066a17fe5e586f9c991b1ac32..95efe0d65bf33015537119aa546f2523797dfcf2 100644 --- a/bin/Release/write.txt +++ b/bin/Release/write.txt @@ -1 +1 @@ -123456789ABCDEF0 \ No newline at end of file +123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0 \ No newline at end of file diff --git a/desfire_example_c++.layout b/desfire_example_c++.layout index ab20f96909c42ef7c201baed13dd59cf85ac56b9..62a122b6eb8ca2368ab196c1cbd8f5617bf88cbb 100644 --- a/desfire_example_c++.layout +++ b/desfire_example_c++.layout @@ -4,7 +4,7 @@ - + diff --git a/lib b/lib new file mode 160000 index 0000000000000000000000000000000000000000..bbe7b1eca27cfadadf06b0abe5d0e36efa32d72f --- /dev/null +++ b/lib @@ -0,0 +1 @@ +Subproject commit bbe7b1eca27cfadadf06b0abe5d0e36efa32d72f diff --git a/main.cpp b/main.cpp index b92bfd6e14a24c025cc4ce6eb6b23ddf2d72ac09..7d17388b4ef2dacfc86f1bb09eeb43eed7baf40a 100644 --- a/main.cpp +++ b/main.cpp @@ -15,8 +15,8 @@ #include #include -#define APP_VERSION "1.7" -#include "ufr-lib/include/uFCoder.h" +#define APP_VERSION "1.8" +#include "lib/include/uFCoder.h" void convert_str_to_key(std::string key_str, unsigned char *key, unsigned char key_length); bool prepare_key(unsigned char *key); @@ -716,7 +716,7 @@ void usage(void) " (r) - Get application IDs\n" " (s) - Store key into SAM\n" " (t) - Change config parameters\n" - " (u) - Get file settings (Desfire light only)\n" + " (u) - Get file settings\n" " (v) - Change file settings (Desfire light only)\n" " (w) - Delete transaction MAC file (Desfire light only)\n" " (x) - Check ECC signature (Desfire EV2 and Desfire light)\n"); @@ -2755,7 +2755,7 @@ void MakeFile(){ scanf("%d%*c", &file_id); - printf("Choose communication mode:\n 1 - PLAIN.\n 2 - MACKED.\n 3 - ENCIPHERED.\n"); + printf("Choose communication mode:\n 1 - PLAIN.\n 2 - MAC.\n 3 - ENCIPHERED.\n"); scanf("%d%*c", &comm_choice); printf("Choose file type:\n 1 - Standard data file\n 2 - Value file\n 3 - Linear record file\n 4 - Cyclic record file\n"); @@ -3322,7 +3322,7 @@ void ReadValueFile() file_id = stoul(settings[3], nullptr, 10); - printf("\nChoose communication mode:\n 1 - PLAIN.\n 2 - MACKED.\n 3 - ENCIPHERED.\n"); + printf("\nChoose communication mode:\n 1 - PLAIN.\n 2 - MAC.\n 3 - ENCIPHERED.\n"); scanf("%d%*c", &comm_choice); switch (comm_choice) { @@ -3456,7 +3456,7 @@ void IncreaseValueFile() file_id = stoul(settings[3], nullptr, 10); - printf("\nChoose communication mode:\n 1 - PLAIN.\n 2 - MACKED.\n 3 - ENCIPHERED.\n"); + printf("\nChoose communication mode:\n 1 - PLAIN.\n 2 - MAC.\n 3 - ENCIPHERED.\n"); scanf("%d%*c", &comm_choice); switch (comm_choice) { @@ -3749,7 +3749,7 @@ void DecreaseValueFile() file_id = stoul(settings[3], nullptr, 10); - printf("\nChoose communication mode:\n 1 - PLAIN.\n 2 - MACKED.\n 3 - ENCIPHERED.\n"); + printf("\nChoose communication mode:\n 1 - PLAIN.\n 2 - MAC.\n 3 - ENCIPHERED.\n"); scanf("%d%*c", &comm_choice); switch (comm_choice) { @@ -4046,7 +4046,7 @@ void WriteStdFile() printf("\nChoose file type:\n 1 - Standard data file\n 2 - Record file\n"); scanf("%d%*c", &file_type); - printf("\nChoose communication mode:\n 1 - PLAIN.\n 2 - MACKED.\n 3 - ENCIPHERED.\n"); + printf("\nChoose communication mode:\n 1 - PLAIN.\n 2 - MAC.\n 3 - ENCIPHERED.\n"); scanf("%d%*c", &comm_choice); switch (comm_choice) { @@ -4390,7 +4390,7 @@ void ReadStdFile() } - printf("\nChoose communication mode:\n 1 - PLAIN.\n 2 - MACKED.\n 3 - ENCIPHERED.\n"); + printf("\nChoose communication mode:\n 1 - PLAIN.\n 2 - MAC.\n 3 - ENCIPHERED.\n"); scanf("%d%*c", &comm_choice); switch (comm_choice) { @@ -5222,31 +5222,153 @@ void GetFileSetting(void) int file_no_int; uint8_t file_no, file_type, communication_mode; uint8_t read_key_no, write_key_no, read_write_key_no, change_key_no; - uint32_t file_size; + uint32_t file_size = 0; int32_t lower_limit, upper_limit; uint32_t limited_credit_value; uint8_t limited_credit_enable, free_get_value; - uint32_t record_size, max_number_of_rec, curr_number_of_rec; + uint32_t record_size = 0; + uint32_t max_number_of_rec = 0; + uint32_t curr_number_of_rec = 0; uint8_t ex_unauth_operation, tmc_limit_conf, tm_key_type, tm_key_version; uint32_t tmc_limit; uint8_t dfl_status; + int card_type; + uint8_t file_settings[20], settings_len; + uint8_t key_ext[24]; + uint16_t card_status, exec_time; std::cout << "Enter file number:" << std::endl; scanf("%d%*c", &file_no_int); file_no = file_no_int & 0xFF; - dfl_status = dfl_get_file_settings(file_no, &file_type, &communication_mode, + printf("\nEnter card type:\n"); + printf("1 - Desfire EV2\n"); + printf("2 - Desfire Light\n"); + scanf("%d%*c", &card_type); + if(card_type == 2) + { + dfl_status = dfl_get_file_settings(file_no, &file_type, &communication_mode, &read_key_no, &write_key_no, &read_write_key_no, &change_key_no, &file_size, &lower_limit, &upper_limit, &limited_credit_value, &limited_credit_enable, &free_get_value, &record_size, &max_number_of_rec, &curr_number_of_rec, &ex_unauth_operation, &tmc_limit_conf, &tm_key_type, &tm_key_version, &tmc_limit); - status = (UFR_STATUS)nt4h_error_codes_to_desfire(dfl_status); - if(status) + status = (UFR_STATUS)nt4h_error_codes_to_desfire(dfl_status); + if(status) + { + std::cout << std::endl << "Get file setting error " << UFR_Status2String(status) << std::endl; + return; + } + } + else { - std::cout << std::endl << "Get file setting error " << UFR_Status2String(status) << std::endl; - return; + aid = strtol(settings[1].c_str(),NULL,16); + + aid_key_nr = stoul(settings[2], nullptr, 10); + + if (internal_key == false) + { + if(sam_key == true) + key_nr = stoul(settings[4], nullptr, 10); + else if (!prepare_key(key_ext)) + { + return; + } + } + else + { + key_nr = stoul(settings[4], nullptr, 10); + } + + if (master_authent_req == true) + { + if (internal_key == true) + { + if(key_type_nr == AES_KEY_TYPE) + status = uFR_int_DesfireGetFileSettings_aes(key_nr, aid, file_no, file_settings, &settings_len, &card_status, &exec_time); + else if(key_type_nr == DES_KEY_TYPE) + status = uFR_int_DesfireGetFileSettings_des(key_nr, aid, file_no, file_settings, &settings_len, &card_status, &exec_time); + else if(key_type_nr == DES2K_KEY_TYPE) + status = uFR_int_DesfireGetFileSettings_2k3des(key_nr, aid, file_no, file_settings, &settings_len, &card_status, &exec_time); + else + status = uFR_int_DesfireGetFileSettings_3k3des(key_nr, aid, file_no, file_settings, &settings_len, &card_status, &exec_time); + } + else + { + if(sam_key == true) + { + if(key_type_nr == AES_KEY_TYPE) + status = uFR_SAM_DesfireGetFileSettingsAesAuth(key_nr, aid, file_no, file_settings, &settings_len, &card_status, &exec_time); + else if(key_type_nr == DES_KEY_TYPE) + status = uFR_SAM_DesfireGetFileSettingsDesAuth(key_nr, aid, file_no, file_settings, &settings_len, &card_status, &exec_time); + else if(key_type_nr == DES2K_KEY_TYPE) + status = uFR_SAM_DesfireGetFileSettings2k3desAuth(key_nr, aid, file_no, file_settings, &settings_len, &card_status, &exec_time); + else + status = uFR_SAM_DesfireGetFileSettings3k3desAuth(key_nr, aid, file_no, file_settings, &settings_len, &card_status, &exec_time); + } + else + { + if(key_type_nr == AES_KEY_TYPE) + status = uFR_int_DesfireGetFileSettings_aes_PK(key_ext, aid, file_no, file_settings, &settings_len, &card_status, &exec_time); + else if(key_type_nr == DES_KEY_TYPE) + status = uFR_int_DesfireGetFileSettings_des_PK(key_ext, aid, file_no, file_settings, &settings_len, &card_status, &exec_time); + else if(key_type_nr == DES2K_KEY_TYPE) + status = uFR_int_DesfireGetFileSettings_2k3des_PK(key_ext, aid, file_no, file_settings, &settings_len, &card_status, &exec_time); + else + status = uFR_int_DesfireGetFileSettings_3k3des_PK(key_ext, aid, file_no, file_settings, &settings_len, &card_status, &exec_time); + } + } + } + else + status = uFR_int_DesfireGetFileSettings_no_auth(aid, file_no, file_settings, &settings_len, &card_status, &exec_time); + + if(status) + { + std::cout << std::endl << "Get file setting error " << UFR_Status2String(status) << std::endl; + return; + } + + std::cout << "Operation completed\n"; + + std::cout << "Function status is: " << UFR_Status2String(status) << std::endl; + + std::cout << "Card status is: " << switch_card_status(card_status) << std::endl; + + std::cout << "Execution time: " << exec_time << " ms" << std::endl; + + if(card_status != CARD_OPERATION_OK) + return; + + file_type = file_settings[0]; + communication_mode = file_settings[1] & 0x03; + read_key_no = file_settings[3] >> 4; + write_key_no = file_settings[3] & 0x0F; + read_write_key_no = file_settings[2] >> 4; + change_key_no = file_settings[2] & 0x0F; + + if(file_type == 0) + { + memcpy(&file_size, &file_settings[4], 3); + } + else if(file_type == 2) + { + memcpy(&lower_limit, &file_settings[4], 4); + memcpy(&upper_limit, &file_settings[8], 4); + memcpy(&limited_credit_value, &file_settings[12], 4); + limited_credit_enable = file_settings[16]; + } + else if(file_type == 4) + { + memcpy(&record_size, &file_settings[4], 3); + memcpy(&max_number_of_rec, &file_settings[7], 3); + memcpy(&curr_number_of_rec, &file_settings[10], 3); + } + else + { + tm_key_type = file_settings[4]; + tm_key_version = file_settings[5]; + } } switch(file_type) @@ -5271,7 +5393,7 @@ void GetFileSetting(void) printf("Plain communication mode\n"); break; case 1: - printf("Macked communication mode\n"); + printf("Maced communication mode\n"); break; case 3: printf("Enciphered communication mode\n"); @@ -5364,7 +5486,7 @@ void ChangeFileSetting(void) printf("Enter Change key number: "); scanf("%d%*c", &change_key_nr); - printf("Choose new communication mode:\n 1 - PLAIN.\n 2 - MACKED.\n 3 - ENCIPHERED.\n"); + printf("Choose new communication mode:\n 1 - PLAIN.\n 2 - MAC.\n 3 - ENCIPHERED.\n"); scanf("%d%*c", &comm_choice); switch (comm_choice) diff --git a/obj/Debug/main.o b/obj/Debug/main.o index d5f2819cee00b59977c38041f2fc3cbd72b5d5d4..42da6b9062e4730a21c2c973ce4da51afb7fe23b 100644 Binary files a/obj/Debug/main.o and b/obj/Debug/main.o differ diff --git a/obj/Release/main.o b/obj/Release/main.o index 14446c2a9a30717cefbcab0e268f610f649f9f0b..6b0655c30a22e8bc766cfaffbd5610aaacca4ba6 100644 Binary files a/obj/Release/main.o and b/obj/Release/main.o differ