diff --git a/libfreefare/mifare_desfire.c b/libfreefare/mifare_desfire.c index 9b887d7..27ea4e3 100644 --- a/libfreefare/mifare_desfire.c +++ b/libfreefare/mifare_desfire.c @@ -1353,6 +1353,7 @@ write_data (MifareTag tag, uint8_t command, uint8_t file_no, off_t offset, size_ BUFFER_APPEND_BYTES (cmd, data, length); uint8_t *p = mifare_cryto_preprocess_data (tag, cmd, &__cmd_n, 8, cs | MAC_COMMAND | CMAC_COMMAND | ENC_COMMAND); + size_t overhead_size = __cmd_n - length; // (CRC | padding) + headers BUFFER_INIT(d, FRAME_PAYLOAD_SIZE); bytes_left = FRAME_PAYLOAD_SIZE - 8; @@ -1379,7 +1380,7 @@ write_data (MifareTag tag, uint8_t command, uint8_t file_no, off_t offset, size_ if (0x00 == p[__res_n-1]) { // Remove header length - bytes_send -= 8; + bytes_send -= overhead_size; } else { // 0xAF (additionnal Frame) failure can happen here (wrong crypto method). MIFARE_DESFIRE (tag)->last_picc_error = p[__res_n-1]; diff --git a/test/test_mifare_desfire_ev1_aes.c b/test/test_mifare_desfire_ev1_aes.c index 1435ca5..36a0c42 100644 --- a/test/test_mifare_desfire_ev1_aes.c +++ b/test/test_mifare_desfire_ev1_aes.c @@ -56,8 +56,6 @@ test_mifare_desfire_ev1_aes (void) */ res = mifare_desfire_change_key_settings (tag, 0xF); cut_assert_success ("mifare_desfire_change_key_settings()"); - res = mifare_desfire_change_key_settings (tag, 0xF); - cut_assert_success ("mifare_desfire_change_key_settings()"); /* Change master key to AES */ MifareDESFireKey key = mifare_desfire_aes_key_new_with_version (key_data_aes, key_data_aes_version); @@ -828,3 +826,43 @@ test_mifare_desfire_ev1_aes (void) free (aid_d); } +void +test_mifare_desfire_ev1_aes_write_data_encyphered (void) +{ + int res; + + mifare_desfire_auto_authenticate (tag, 0); + + /* Wipeout the card */ + res = mifare_desfire_format_picc (tag); + cut_assert_success ("mifare_desfire_format_picc()"); + + MifareDESFireAID aid = mifare_desfire_aid_new (0x112233); + res = mifare_desfire_create_application (tag, aid, 0x0F, 0x83); + cut_assert_success ("mifare_desfire_create_application()"); + + res = mifare_desfire_select_application (tag, aid); + cut_assert_success ("mifare_desfire_select_application()"); + + MifareDESFireKey key = mifare_desfire_aes_key_new (key_data_aes); + res = mifare_desfire_authenticate_aes (tag, 0, key); + cut_assert_success ("mifare_desfire_authenticate_aes()"); + mifare_desfire_key_free (key); + + res = mifare_desfire_create_std_data_file (tag, 9, MDCM_ENCIPHERED, 0x0000, 7); + cut_assert_success ("mifare_desfire_create_std_data_file()"); + + res = mifare_desfire_write_data (tag, 9, 0, 5, "Hello"); + cut_assert_equal_int (5, res, cut_message ("Wrong value returned")); + + res = mifare_desfire_select_application (tag, NULL); + cut_assert_success ("mifare_desfire_select_application()"); + + mifare_desfire_auto_authenticate (tag, 0); + + /* Wipeout the card */ + res = mifare_desfire_format_picc (tag); + cut_assert_success ("mifare_desfire_format_picc()"); + + free (aid); +}