diff --git a/applications/main/nfc/nfc_cli.c b/applications/main/nfc/nfc_cli.c index 6e6e04ca9..6e14e371f 100644 --- a/applications/main/nfc/nfc_cli.c +++ b/applications/main/nfc/nfc_cli.c @@ -34,13 +34,25 @@ static void nfc_cli_detect(Cli* cli, FuriString* args) { while(!cmd_exit) { cmd_exit |= cli_cmd_interrupt_received(cli); if(furi_hal_nfc_detect(&dev_data, 400)) { - printf("Found: %s ", nfc_get_dev_type(dev_data.type)); - printf("UID length: %d, UID:", dev_data.uid_len); - for(size_t i = 0; i < dev_data.uid_len; i++) { - printf("%02X", dev_data.uid[i]); + if(dev_data.type == FuriHalNfcTypeA) { + printf("UID length: %d, UID:", dev_data.uid_len); + for(size_t i = 0; i < dev_data.uid_len; i++) { + printf("%02X", dev_data.uid[i]); + } + printf("\r\n"); + break; + } else if(dev_data.type == FuriHalNfcTypeF) { + printf("IDm:"); + for(size_t i = 0; i < 8; i++) { + printf("%02X", dev_data.uid[i]); + } + printf("\r\nPMm:"); + for(size_t i = 0; i < 8; i++) { + printf("%02X", dev_data.f_data.pmm[i]); + } + printf("\r\n"); + break; } - printf("\r\n"); - break; } furi_hal_nfc_sleep(); furi_delay_ms(50); @@ -63,13 +75,17 @@ static void nfc_cli_emulate(Cli* cli, FuriString* args) { FuriHalNfcDevData params = { .uid = {0x36, 0x9C, 0xe7, 0xb1, 0x0A, 0xC1, 0x34}, .uid_len = 7, - .atqa = {0x44, 0x00}, - .sak = 0x00, + .a_data = + { + .atqa = {0x44, 0x00}, + .sak = 0x00, + }, .type = FuriHalNfcTypeA, }; while(!cli_cmd_interrupt_received(cli)) { - if(furi_hal_nfc_listen(params.uid, params.uid_len, params.atqa, params.sak, false, 100)) { + if(furi_hal_nfc_listen( + params.uid, params.uid_len, params.a_data.atqa, params.a_data.sak, false, 100)) { printf("Reader detected\r\n"); furi_hal_nfc_sleep(); } diff --git a/applications/main/nfc/nfc_i.h b/applications/main/nfc/nfc_i.h index f7e489902..7331b9405 100644 --- a/applications/main/nfc/nfc_i.h +++ b/applications/main/nfc/nfc_i.h @@ -39,13 +39,23 @@ #include "rpc/rpc_app.h" +#include #include -ARRAY_DEF(MfClassicUserKeys, char*, M_PTR_OPLIST); +ARRAY_DEF(FelicaAreaPath, FelicaArea*, M_PTR_OPLIST) +ARRAY_DEF(MfClassicUserKeys, char*, M_PTR_OPLIST) #define NFC_TEXT_STORE_SIZE 128 #define NFC_APP_FOLDER ANY_PATH("nfc") +typedef struct { + FelicaSystem* selected_system; + + FelicaAreaPath_t selected_areas; + + FelicaService* selected_service; +} FelicaSelectState; + typedef enum { NfcRpcStateIdle, NfcRpcStateEmulating, @@ -65,6 +75,7 @@ struct Nfc { FuriString* text_box_store; uint8_t byte_input_store[6]; MfClassicUserKeys_t mfc_key_strs; // Used in MFC key listing + FelicaSelectState felica_select; void* rpc_ctx; NfcRpcState rpc_state; diff --git a/applications/main/nfc/scenes/nfc_scene_config.h b/applications/main/nfc/scenes/nfc_scene_config.h index d6edebe73..c02e83787 100644 --- a/applications/main/nfc/scenes/nfc_scene_config.h +++ b/applications/main/nfc/scenes/nfc_scene_config.h @@ -52,6 +52,10 @@ ADD_SCENE(nfc, mf_classic_update_success, MfClassicUpdateSuccess) ADD_SCENE(nfc, mf_classic_wrong_card, MfClassicWrongCard) ADD_SCENE(nfc, emv_read_success, EmvReadSuccess) ADD_SCENE(nfc, emv_menu, EmvMenu) +ADD_SCENE(nfc, felica_read_success, FelicaReadSuccess) +ADD_SCENE(nfc, felica_menu, FelicaMenu) +ADD_SCENE(nfc, felica_info_select, FelicaInfoSelect) +ADD_SCENE(nfc, felica_service_data, FelicaServiceData) ADD_SCENE(nfc, emulate_apdu_sequence, EmulateApduSequence) ADD_SCENE(nfc, device_info, DeviceInfo) ADD_SCENE(nfc, delete, Delete) diff --git a/applications/main/nfc/scenes/nfc_scene_delete.c b/applications/main/nfc/scenes/nfc_scene_delete.c index 0808db45a..8b712e948 100644 --- a/applications/main/nfc/scenes/nfc_scene_delete.c +++ b/applications/main/nfc/scenes/nfc_scene_delete.c @@ -44,6 +44,8 @@ void nfc_scene_delete_on_enter(void* context) { } else if(protocol == NfcDeviceProtocolNfcV) { furi_string_set(temp_str, "ISO15693 tag"); nfc_type = "NFC-V"; + } else if(protocol == NfcDeviceProtocolFelica) { + furi_string_set(temp_str, "FeliCa"); } else { furi_string_set(temp_str, "Unknown ISO tag"); } diff --git a/applications/main/nfc/scenes/nfc_scene_felica_info_select.c b/applications/main/nfc/scenes/nfc_scene_felica_info_select.c new file mode 100644 index 000000000..3b7c570d7 --- /dev/null +++ b/applications/main/nfc/scenes/nfc_scene_felica_info_select.c @@ -0,0 +1,151 @@ +#include "../nfc_i.h" +#include + +void nfc_scene_felica_info_select_submenu_callback(void* context, uint32_t index) { + Nfc* nfc = context; + + view_dispatcher_send_custom_event(nfc->view_dispatcher, index); +} + +void nfc_scene_felica_info_select_on_enter(void* context) { + Nfc* nfc = context; + Submenu* submenu = nfc->submenu; + FelicaData* data = &nfc->dev->dev_data.felica_data; + FelicaSelectState* state = &nfc->felica_select; + + FelicaAreaPath_init(nfc->felica_select.selected_areas); + + submenu_add_item(submenu, "[Actions]", 0, nfc_scene_felica_info_select_submenu_callback, nfc); + uint8_t i = 1; + if(state->selected_system == NULL || state->selected_system->code == LITE_SYSTEM_CODE) { + submenu_set_header(submenu, "Systems"); + FelicaSystemList_it_t it; + for(FelicaSystemList_it(it, data->systems); !FelicaSystemList_end_p(it); + FelicaSystemList_next(it)) { + FelicaSystem* current_system = *FelicaSystemList_ref(it); + FuriString* system_name = felica_get_system_name(current_system); + submenu_add_item( + submenu, + furi_string_get_cstr(system_name), + i++, + nfc_scene_felica_info_select_submenu_callback, + nfc); + furi_string_free(system_name); + } + } else { + FelicaSystem* system = state->selected_system; + FuriString* header = furi_string_alloc_printf("%04X/", system->code); + + FelicaArea* area = &system->root_area; + if(FelicaAreaPath_size(state->selected_areas) > 0) { + FelicaAreaPath_it_t it; + for(FelicaAreaPath_it(it, state->selected_areas); !FelicaAreaPath_end_p(it); + FelicaAreaPath_next(it)) { + FelicaArea* ancestor = *FelicaAreaPath_ref(it); + furi_string_cat_printf(header, "%d/", ancestor->number); + } + area = *FelicaAreaPath_back(state->selected_areas); + } + furi_string_cat(header, "Areas"); + + submenu_set_header(submenu, furi_string_get_cstr(header)); + furi_string_free(header); + + FelicaNodeList_it_t it; + for(FelicaNodeList_it(it, area->nodes); !FelicaNodeList_end_p(it); + FelicaNodeList_next(it)) { + FelicaNode* node = *FelicaNodeList_ref(it); + FuriString* node_name = furi_string_alloc(); + if(node->type == FelicaNodeTypeArea) { + furi_string_printf(node_name, "Area %d", node->area->number); + submenu_add_item( + submenu, + furi_string_get_cstr(node_name), + i++, + nfc_scene_felica_info_select_submenu_callback, + nfc); + } else { + uint16_t service_code = node->service->number << 6; + furi_string_printf(node_name, "Service %04X", service_code); + submenu_add_item( + submenu, + furi_string_get_cstr(node_name), + i++, + nfc_scene_felica_info_select_submenu_callback, + nfc); + } + + furi_string_free(node_name); + } + } + + state->selected_service = NULL; + submenu_set_selected_item( + nfc->submenu, scene_manager_get_scene_state(nfc->scene_manager, NfcSceneFelicaInfoSelect)); + + view_dispatcher_switch_to_view(nfc->view_dispatcher, NfcViewMenu); +} + +bool nfc_scene_felica_info_select_on_event(void* context, SceneManagerEvent event) { + Nfc* nfc = context; + FelicaData* data = &nfc->dev->dev_data.felica_data; + FelicaSelectState* state = &nfc->felica_select; + bool consumed = false; + + if(event.type == SceneManagerEventTypeCustom) { + uint8_t index = event.event; + scene_manager_set_scene_state(nfc->scene_manager, NfcSceneMfUltralightMenu, index); + + if(index == 0) { + return false; + } + + index -= 1; + if(state->selected_system == NULL) { + state->selected_system = *FelicaSystemList_get(data->systems, index); + if(state->selected_system->code == LITE_SYSTEM_CODE) { + scene_manager_next_scene(nfc->scene_manager, NfcSceneFelicaServiceData); + } else { + scene_manager_next_scene(nfc->scene_manager, NfcSceneFelicaInfoSelect); + } + consumed = true; + } else { + FelicaNode* selected_node = NULL; + if(FelicaAreaPath_size(state->selected_areas) == 0) { + selected_node = + *FelicaNodeList_get(state->selected_system->root_area.nodes, index); + } else { + FelicaArea* current_area = *FelicaAreaPath_back(state->selected_areas); + selected_node = *FelicaNodeList_get(current_area->nodes, index); + } + + if(selected_node->type == FelicaNodeTypeArea) { + FelicaAreaPath_push_back(state->selected_areas, selected_node->area); + scene_manager_next_scene(nfc->scene_manager, NfcSceneFelicaInfoSelect); + consumed = true; + } else if(selected_node->type == FelicaNodeTypeService) { + state->selected_service = selected_node->service; + scene_manager_next_scene(nfc->scene_manager, NfcSceneFelicaServiceData); + consumed = true; + } + } + } else if(event.type == SceneManagerEventTypeBack) { + if(FelicaAreaPath_size(state->selected_areas) <= 1) { + FelicaAreaPath_clear(state->selected_areas); + state->selected_system = NULL; + } else { + FelicaAreaPath_pop_back(NULL, state->selected_areas); + } + consumed = scene_manager_previous_scene(nfc->scene_manager); + } + + return consumed; +} + +void nfc_scene_felica_info_select_on_exit(void* context) { + Nfc* nfc = context; + + // Clear view + FelicaAreaPath_clear(nfc->felica_select.selected_areas); + submenu_reset(nfc->submenu); +} diff --git a/applications/main/nfc/scenes/nfc_scene_felica_menu.c b/applications/main/nfc/scenes/nfc_scene_felica_menu.c new file mode 100644 index 000000000..b989047d6 --- /dev/null +++ b/applications/main/nfc/scenes/nfc_scene_felica_menu.c @@ -0,0 +1,84 @@ +#include "../nfc_i.h" +#include + +enum SubmenuIndex { + /* + SubmenuIndexUnlock, + SubmenuIndexSave, + SubmenuIndexEmulate, + */ + SubmenuIndexInfo, +}; + +void nfc_scene_felica_menu_submenu_callback(void* context, uint32_t index) { + Nfc* nfc = context; + + view_dispatcher_send_custom_event(nfc->view_dispatcher, index); +} + +void nfc_scene_felica_menu_on_enter(void* context) { + Nfc* nfc = context; + Submenu* submenu = nfc->submenu; + // FelicaData* data = &nfc->dev->dev_data.felica_data; + + /* + submenu_add_item( + submenu, "Unlock", SubmenuIndexUnlock, nfc_scene_felica_menu_submenu_callback, nfc); + submenu_add_item( + submenu, "Save", SubmenuIndexSave, nfc_scene_felica_menu_submenu_callback, nfc); + submenu_add_item( + submenu, "Emulate", SubmenuIndexEmulate, nfc_scene_felica_menu_submenu_callback, nfc); + */ + submenu_add_item( + submenu, "Info", SubmenuIndexInfo, nfc_scene_felica_menu_submenu_callback, nfc); + + submenu_set_selected_item( + nfc->submenu, scene_manager_get_scene_state(nfc->scene_manager, NfcSceneFelicaMenu)); + + view_dispatcher_switch_to_view(nfc->view_dispatcher, NfcViewMenu); +} + +bool nfc_scene_felica_menu_on_event(void* context, SceneManagerEvent event) { + Nfc* nfc = context; + bool consumed = false; + + if(event.type == SceneManagerEventTypeCustom) { + /* + if(event.event == SubmenuIndexSave) { + nfc->dev->format = NfcDeviceSaveFormatFelica; + // Clear device name + nfc_device_set_name(nfc->dev, ""); + scene_manager_next_scene(nfc->scene_manager, NfcSceneSaveName); + consumed = true; + } else if(event.event == SubmenuIndexEmulate) { + scene_manager_next_scene(nfc->scene_manager, NfcSceneFelicaEmulate); + if(scene_manager_has_previous_scene(nfc->scene_manager, NfcSceneSetType)) { + DOLPHIN_DEED(DolphinDeedNfcAddEmulate); + } else { + DOLPHIN_DEED(DolphinDeedNfcEmulate); + } + consumed = true; + } else if(event.event == SubmenuIndexUnlock) { + scene_manager_next_scene(nfc->scene_manager, NfcSceneFelicaUnlockMenu); + consumed = true; + } else + */ + if(event.event == SubmenuIndexInfo) { + scene_manager_next_scene(nfc->scene_manager, NfcSceneFelicaInfoSelect); + consumed = true; + } + scene_manager_set_scene_state(nfc->scene_manager, NfcSceneFelicaMenu, event.event); + + } else if(event.type == SceneManagerEventTypeBack) { + consumed = scene_manager_previous_scene(nfc->scene_manager); + } + + return consumed; +} + +void nfc_scene_felica_menu_on_exit(void* context) { + Nfc* nfc = context; + + // Clear view + submenu_reset(nfc->submenu); +} diff --git a/applications/main/nfc/scenes/nfc_scene_felica_read_success.c b/applications/main/nfc/scenes/nfc_scene_felica_read_success.c new file mode 100644 index 000000000..52bba0ee3 --- /dev/null +++ b/applications/main/nfc/scenes/nfc_scene_felica_read_success.c @@ -0,0 +1,83 @@ +#include "../nfc_i.h" + +void nfc_scene_felica_read_success_widget_callback( + GuiButtonType result, + InputType type, + void* context) { + furi_assert(context); + Nfc* nfc = context; + + if(type == InputTypeShort) { + view_dispatcher_send_custom_event(nfc->view_dispatcher, result); + } +} + +void nfc_scene_felica_read_success_on_enter(void* context) { + Nfc* nfc = context; + FelicaData* felica_data = &nfc->dev->dev_data.felica_data; + + // Setup view + Widget* widget = nfc->widget; + widget_add_button_element( + widget, GuiButtonTypeLeft, "Retry", nfc_scene_felica_read_success_widget_callback, nfc); + widget_add_button_element( + widget, GuiButtonTypeRight, "More", nfc_scene_felica_read_success_widget_callback, nfc); + + FuriString* temp_str = NULL; + if(furi_string_size(nfc->dev->dev_data.parsed_data)) { + temp_str = furi_string_alloc_set(nfc->dev->dev_data.parsed_data); + } else { + temp_str = furi_string_alloc_printf("\e#%s", nfc_felica_type(felica_data->type)); + + FelicaSystemList_it_t it; + for(FelicaSystemList_it(it, felica_data->systems); !FelicaSystemList_end_p(it); + FelicaSystemList_next(it)) { + FelicaSystem* current_system = *FelicaSystemList_ref(it); + furi_string_cat_printf( + temp_str, "\nSystem %04X (#%d):", current_system->code, current_system->number); + furi_string_cat_printf(temp_str, "\nIDm:\n "); + for(size_t i = 0; i < 8; i++) { + furi_string_cat_printf(temp_str, "%02X", current_system->idm[i]); + } + furi_string_cat_printf(temp_str, "\nPMm:\n "); + for(size_t i = 0; i < 8; i++) { + furi_string_cat_printf(temp_str, "%02X", current_system->pmm[i]); + } + } + } + + widget_add_text_scroll_element(widget, 0, 0, 128, 52, furi_string_get_cstr(temp_str)); + furi_string_free(temp_str); + + notification_message_block(nfc->notifications, &sequence_set_green_255); + + view_dispatcher_switch_to_view(nfc->view_dispatcher, NfcViewWidget); +} + +bool nfc_scene_felica_read_success_on_event(void* context, SceneManagerEvent event) { + Nfc* nfc = context; + bool consumed = false; + + if(event.type == SceneManagerEventTypeCustom) { + if(event.event == GuiButtonTypeLeft) { + scene_manager_next_scene(nfc->scene_manager, NfcSceneRetryConfirm); + consumed = true; + } else if(event.event == GuiButtonTypeRight) { + scene_manager_next_scene(nfc->scene_manager, NfcSceneFelicaMenu); + consumed = true; + } + } else if(event.type == SceneManagerEventTypeBack) { + scene_manager_next_scene(nfc->scene_manager, NfcSceneExitConfirm); + consumed = true; + } + return consumed; +} + +void nfc_scene_felica_read_success_on_exit(void* context) { + Nfc* nfc = context; + + notification_message_block(nfc->notifications, &sequence_reset_green); + + // Clear view + widget_reset(nfc->widget); +} diff --git a/applications/main/nfc/scenes/nfc_scene_felica_service_data.c b/applications/main/nfc/scenes/nfc_scene_felica_service_data.c new file mode 100644 index 000000000..5153d7d37 --- /dev/null +++ b/applications/main/nfc/scenes/nfc_scene_felica_service_data.c @@ -0,0 +1,104 @@ +#include "../nfc_i.h" + +void nfc_scene_felica_service_data_on_enter(void* context) { + Nfc* nfc = context; + FelicaSelectState* select_state = &nfc->felica_select; + FelicaSystem* system = select_state->selected_system; + //FelicaService* service = select_state->selected_service; + TextBox* text_box = nfc->text_box; + + if(system->code == LITE_SYSTEM_CODE) { + FelicaLiteInfo* lite_info = &system->lite_info; + uint8_t* data; + text_box_set_font(text_box, TextBoxFontHex); + furi_string_cat_str(nfc->text_box_store, "S_PAD:\n"); + for(int i = 0; i < REG_LITE_BLOCK; i++) { + data = lite_info->S_PAD[i]; + for(uint16_t i = 0; i < FELICA_BLOCK_SIZE; i += 2) { + if(!(i % 8) && i) { + furi_string_push_back(nfc->text_box_store, '\n'); + } + if(data != NULL) { + furi_string_cat_printf(nfc->text_box_store, "%02X%02X ", data[i], data[i + 1]); + } else { + furi_string_cat_printf(nfc->text_box_store, "???? "); + } + } + } + + furi_string_cat_str(nfc->text_box_store, "REG:\n"); + data = lite_info->REG; + for(uint16_t i = 0; i < FELICA_BLOCK_SIZE; i += 2) { + if(!(i % 8) && i) { + furi_string_push_back(nfc->text_box_store, '\n'); + } + if(data != NULL) { + furi_string_cat_printf(nfc->text_box_store, "%02X%02X ", data[i], data[i + 1]); + } else { + furi_string_cat_printf(nfc->text_box_store, "???? "); + } + } + + furi_string_cat_str(nfc->text_box_store, "MAC:\n"); + data = lite_info->MAC; + for(uint16_t i = 0; i < 8; i += 2) { + if(!(i % 8) && i) { + furi_string_push_back(nfc->text_box_store, '\n'); + } + furi_string_cat_printf(nfc->text_box_store, "%02X%02X ", data[i], data[i + 1]); + } + + furi_string_cat_printf(nfc->text_box_store, "DFC: %04X\n", lite_info->data_format_code); + + furi_string_cat_str(nfc->text_box_store, "ID data:\n"); + data = lite_info->ID_value; + for(uint16_t i = 0; i < 6; i += 2) { + furi_string_cat_printf(nfc->text_box_store, "%02X%02X ", data[i], data[i + 1]); + } + furi_string_cat_str(nfc->text_box_store, "\n"); + + furi_string_cat_printf(nfc->text_box_store, "CKV: %04X\n", lite_info->card_key_version); + + furi_string_cat_str(nfc->text_box_store, "MC:\n"); + data = lite_info->memory_config; + for(uint16_t i = 0; i < FELICA_BLOCK_SIZE; i += 2) { + if(!(i % 8) && i) { + furi_string_push_back(nfc->text_box_store, '\n'); + } + furi_string_cat_printf(nfc->text_box_store, "%02X%02X ", data[i], data[i + 1]); + } + + furi_string_cat_printf(nfc->text_box_store, "WCNT: %06lX\n", lite_info->write_count); + + furi_string_cat_str(nfc->text_box_store, "MAC_A:\n"); + data = lite_info->MAC_A; + for(uint16_t i = 0; i < 8; i += 2) { + if(!(i % 8) && i) { + furi_string_push_back(nfc->text_box_store, '\n'); + } + furi_string_cat_printf(nfc->text_box_store, "%02X%02X ", data[i], data[i + 1]); + } + } + text_box_set_text(text_box, furi_string_get_cstr(nfc->text_box_store)); + + view_dispatcher_switch_to_view(nfc->view_dispatcher, NfcViewTextBox); +} + +bool nfc_scene_felica_service_data_on_event(void* context, SceneManagerEvent event) { + Nfc* nfc = context; + bool consumed = false; + + if(event.type == SceneManagerEventTypeBack) { + consumed = scene_manager_previous_scene(nfc->scene_manager); + } + + return consumed; +} + +void nfc_scene_felica_service_data_on_exit(void* context) { + Nfc* nfc = context; + + // Clean view + text_box_reset(nfc->text_box); + furi_string_reset(nfc->text_box_store); +} \ No newline at end of file diff --git a/applications/main/nfc/scenes/nfc_scene_nfc_data_info.c b/applications/main/nfc/scenes/nfc_scene_nfc_data_info.c index b7405f8e2..6d05c6b99 100644 --- a/applications/main/nfc/scenes/nfc_scene_nfc_data_info.c +++ b/applications/main/nfc/scenes/nfc_scene_nfc_data_info.c @@ -201,15 +201,16 @@ void nfc_scene_nfc_data_info_on_enter(void* context) { break; } } else { - char iso_type = FURI_BIT(nfc_data->sak, 5) ? '4' : '3'; + char iso_type = FURI_BIT(nfc_data->a_data.sak, 5) ? '4' : '3'; furi_string_cat_printf(temp_str, "ISO 14443-%c (NFC-A)\n", iso_type); furi_string_cat_printf(temp_str, "UID:"); for(size_t i = 0; i < nfc_data->uid_len; i++) { furi_string_cat_printf(temp_str, " %02X", nfc_data->uid[i]); } furi_string_cat_printf( - temp_str, "\nATQA: %02X %02X ", nfc_data->atqa[1], nfc_data->atqa[0]); - furi_string_cat_printf(temp_str, " SAK: %02X", nfc_data->sak); + + temp_str, "\nATQA: %02X %02X ", nfc_data->a_data.atqa[1], nfc_data->a_data.atqa[0]); + furi_string_cat_printf(temp_str, " SAK: %02X", nfc_data->a_data.sak); } // Set application specific data diff --git a/applications/main/nfc/scenes/nfc_scene_nfca_read_success.c b/applications/main/nfc/scenes/nfc_scene_nfca_read_success.c index a38f31a98..74fff367f 100644 --- a/applications/main/nfc/scenes/nfc_scene_nfca_read_success.c +++ b/applications/main/nfc/scenes/nfc_scene_nfca_read_success.c @@ -24,14 +24,15 @@ void nfc_scene_nfca_read_success_on_enter(void* context) { notification_message_block(nfc->notifications, &sequence_set_green_255); - char iso_type = FURI_BIT(data->sak, 5) ? '4' : '3'; + char iso_type = FURI_BIT(data->a_data.sak, 5) ? '4' : '3'; furi_string_cat_printf(temp_str, "ISO 14443-%c (NFC-A)\n", iso_type); furi_string_cat_printf(temp_str, "UID:"); for(size_t i = 0; i < data->uid_len; i++) { furi_string_cat_printf(temp_str, " %02X", data->uid[i]); } - furi_string_cat_printf(temp_str, "\nATQA: %02X %02X ", data->atqa[1], data->atqa[0]); - furi_string_cat_printf(temp_str, " SAK: %02X", data->sak); + furi_string_cat_printf( + temp_str, "\nATQA: %02X %02X ", data->a_data.atqa[1], data->a_data.atqa[0]); + furi_string_cat_printf(temp_str, " SAK: %02X", data->a_data.sak); widget_add_text_scroll_element(widget, 0, 0, 128, 52, furi_string_get_cstr(temp_str)); furi_string_free(temp_str); diff --git a/applications/main/nfc/scenes/nfc_scene_read.c b/applications/main/nfc/scenes/nfc_scene_read.c index d30706c5b..c96562275 100644 --- a/applications/main/nfc/scenes/nfc_scene_read.c +++ b/applications/main/nfc/scenes/nfc_scene_read.c @@ -102,6 +102,11 @@ bool nfc_scene_read_on_event(void* context, SceneManagerEvent event) { scene_manager_next_scene(nfc->scene_manager, NfcSceneDictNotFound); } consumed = true; + } else if(event.event == NfcWorkerEventReadFelica) { + notification_message(nfc->notifications, &sequence_success); + scene_manager_next_scene(nfc->scene_manager, NfcSceneFelicaReadSuccess); + DOLPHIN_DEED(DolphinDeedNfcReadSuccess); + consumed = true; } else if(event.event == NfcWorkerEventCardDetected) { nfc_scene_read_set_state(nfc, NfcSceneReadStateReading); nfc_blink_detect_start(nfc); diff --git a/applications/main/nfc/scenes/nfc_scene_read_card_type.c b/applications/main/nfc/scenes/nfc_scene_read_card_type.c index 94262aa1e..865b3f54b 100644 --- a/applications/main/nfc/scenes/nfc_scene_read_card_type.c +++ b/applications/main/nfc/scenes/nfc_scene_read_card_type.c @@ -7,6 +7,7 @@ enum SubmenuIndex { SubmenuIndexReadMfUltralight, SubmenuIndexReadEMV, SubmenuIndexReadNFCA, + SubmenuIndexReadFelica, }; void nfc_scene_read_card_type_submenu_callback(void* context, uint32_t index) { @@ -49,6 +50,12 @@ void nfc_scene_read_card_type_on_enter(void* context) { SubmenuIndexReadNFCA, nfc_scene_read_card_type_submenu_callback, nfc); + submenu_add_item( + submenu, + "Read FeliCa", + SubmenuIndexReadFelica, + nfc_scene_read_card_type_submenu_callback, + nfc); uint32_t state = scene_manager_get_scene_state(nfc->scene_manager, NfcSceneReadCardType); submenu_set_selected_item(submenu, state); @@ -85,6 +92,11 @@ bool nfc_scene_read_card_type_on_event(void* context, SceneManagerEvent event) { scene_manager_next_scene(nfc->scene_manager, NfcSceneRead); consumed = true; } + if(event.event == SubmenuIndexReadFelica) { + nfc->dev->dev_data.read_mode = NfcReadModeFelica; + scene_manager_next_scene(nfc->scene_manager, NfcSceneRead); + consumed = true; + } scene_manager_set_scene_state(nfc->scene_manager, NfcSceneReadCardType, event.event); } return consumed; diff --git a/applications/main/nfc/scenes/nfc_scene_set_atqa.c b/applications/main/nfc/scenes/nfc_scene_set_atqa.c index d079b3804..f26ce8436 100644 --- a/applications/main/nfc/scenes/nfc_scene_set_atqa.c +++ b/applications/main/nfc/scenes/nfc_scene_set_atqa.c @@ -17,7 +17,7 @@ void nfc_scene_set_atqa_on_enter(void* context) { nfc_scene_set_atqa_byte_input_callback, NULL, nfc, - nfc->dev->dev_data.nfc_data.atqa, + nfc->dev->dev_data.nfc_data.a_data.atqa, 2); view_dispatcher_switch_to_view(nfc->view_dispatcher, NfcViewByteInput); } diff --git a/applications/main/nfc/scenes/nfc_scene_set_sak.c b/applications/main/nfc/scenes/nfc_scene_set_sak.c index 60a1e1494..98bd6441a 100644 --- a/applications/main/nfc/scenes/nfc_scene_set_sak.c +++ b/applications/main/nfc/scenes/nfc_scene_set_sak.c @@ -17,7 +17,7 @@ void nfc_scene_set_sak_on_enter(void* context) { nfc_scene_set_sak_byte_input_callback, NULL, nfc, - &nfc->dev->dev_data.nfc_data.sak, + &nfc->dev->dev_data.nfc_data.a_data.sak, 1); view_dispatcher_switch_to_view(nfc->view_dispatcher, NfcViewByteInput); } diff --git a/firmware/targets/f7/api_symbols.csv b/firmware/targets/f7/api_symbols.csv index 6768bbf55..340b43a1e 100644 --- a/firmware/targets/f7/api_symbols.csv +++ b/firmware/targets/f7/api_symbols.csv @@ -896,6 +896,21 @@ Function,-,fdim,double,"double, double" Function,-,fdimf,float,"float, float" Function,-,fdiml,long double,"long double, long double" Function,-,fdopen,FILE*,"int, const char*" +Function,-,felica_check_ic_type,_Bool,uint8_t* +Function,-,felica_clear,void,FelicaData* +Function,-,felica_define_normal_block,void,"FelicaService*, uint16_t, uint8_t*" +Function,-,felica_get_ic_type,FelicaICType,uint8_t* +Function,-,felica_get_service_name,FuriString*,FelicaService* +Function,-,felica_get_system_name,FuriString*,FelicaSystem* +Function,-,felica_lite_can_read_without_mac,_Bool,"uint8_t*, uint8_t" +Function,-,felica_lite_prepare_unencrypted_read,uint8_t,"uint8_t*, const FelicaReader*, _Bool, const uint8_t*, uint8_t" +Function,-,felica_lite_prepare_unencrypted_write,uint8_t,"uint8_t*, const FelicaReader*, const uint8_t*, uint8_t, const uint8_t*" +Function,-,felica_parse_unencrypted_read,uint16_t,"uint8_t*, uint8_t, FelicaReader*, uint8_t*, uint16_t" +Function,-,felica_parse_unencrypted_write,_Bool,"uint8_t*, uint8_t, FelicaReader*" +Function,-,felica_prepare_unencrypted_read,uint8_t,"uint8_t*, const FelicaReader*, const uint16_t*, uint8_t, const uint32_t*, uint8_t" +Function,-,felica_prepare_unencrypted_write,uint8_t,"uint8_t*, FelicaReader*, const uint16_t*, uint8_t, const uint32_t*, uint8_t, const uint8_t*" +Function,-,felica_read_card,_Bool,"FuriHalNfcTxRxContext*, FelicaData*, uint8_t*, uint8_t*" +Function,-,felica_read_lite_system,_Bool,"FuriHalNfcTxRxContext*, FelicaReader*, FelicaData*, FelicaSystem*" Function,-,feof,int,FILE* Function,-,feof_unlocked,int,FILE* Function,-,ferror,int,FILE* @@ -2011,7 +2026,7 @@ Function,-,mf_classic_auth_write_block,_Bool,"FuriHalNfcTxRxContext*, MfClassicB Function,-,mf_classic_authenticate,_Bool,"FuriHalNfcTxRxContext*, uint8_t, uint64_t, MfClassicKey" Function,-,mf_classic_authenticate_skip_activate,_Bool,"FuriHalNfcTxRxContext*, uint8_t, uint64_t, MfClassicKey, _Bool, uint32_t" Function,-,mf_classic_block_to_value,_Bool,"const uint8_t*, int32_t*, uint8_t*" -Function,-,mf_classic_check_card_type,_Bool,"uint8_t, uint8_t, uint8_t" +Function,-,mf_classic_check_card_type,_Bool,FuriHalNfcADevData* Function,-,mf_classic_dict_add_key,_Bool,"MfClassicDict*, uint8_t*" Function,-,mf_classic_dict_add_key_str,_Bool,"MfClassicDict*, FuriString*" Function,-,mf_classic_dict_alloc,MfClassicDict*,MfClassicDictType @@ -2029,7 +2044,7 @@ Function,-,mf_classic_dict_is_key_present,_Bool,"MfClassicDict*, uint8_t*" Function,-,mf_classic_dict_is_key_present_str,_Bool,"MfClassicDict*, FuriString*" Function,-,mf_classic_dict_rewind,_Bool,MfClassicDict* Function,-,mf_classic_emulator,_Bool,"MfClassicEmulator*, FuriHalNfcTxRxContext*" -Function,-,mf_classic_get_classic_type,MfClassicType,"uint8_t, uint8_t, uint8_t" +Function,-,mf_classic_get_classic_type,MfClassicType,FuriHalNfcADevData* Function,-,mf_classic_get_read_sectors_and_keys,void,"MfClassicData*, uint8_t*, uint8_t*" Function,-,mf_classic_get_sector_by_block,uint8_t,uint8_t Function,-,mf_classic_get_sector_trailer_block_num_by_sector,uint8_t,uint8_t @@ -2070,7 +2085,7 @@ Function,-,mf_df_cat_file,void,"MifareDesfireFile*, FuriString*" Function,-,mf_df_cat_free_mem,void,"MifareDesfireFreeMemory*, FuriString*" Function,-,mf_df_cat_key_settings,void,"MifareDesfireKeySettings*, FuriString*" Function,-,mf_df_cat_version,void,"MifareDesfireVersion*, FuriString*" -Function,-,mf_df_check_card_type,_Bool,"uint8_t, uint8_t, uint8_t" +Function,-,mf_df_check_card_type,_Bool,FuriHalNfcADevData* Function,-,mf_df_clear,void,MifareDesfireData* Function,-,mf_df_parse_get_application_ids_response,_Bool,"uint8_t*, uint16_t, MifareDesfireApplication**" Function,-,mf_df_parse_get_file_ids_response,_Bool,"uint8_t*, uint16_t, MifareDesfireFile**" @@ -2093,7 +2108,7 @@ Function,-,mf_df_prepare_read_data,uint16_t,"uint8_t*, uint8_t, uint32_t, uint32 Function,-,mf_df_prepare_read_records,uint16_t,"uint8_t*, uint8_t, uint32_t, uint32_t" Function,-,mf_df_prepare_select_application,uint16_t,"uint8_t*, uint8_t[3]" Function,-,mf_df_read_card,_Bool,"FuriHalNfcTxRxContext*, MifareDesfireData*" -Function,-,mf_ul_check_card_type,_Bool,"uint8_t, uint8_t, uint8_t" +Function,-,mf_ul_check_card_type,_Bool,FuriHalNfcADevData* Function,-,mf_ul_is_full_capture,_Bool,MfUltralightData* Function,-,mf_ul_prepare_emulation,void,"MfUltralightEmulator*, MfUltralightData*" Function,-,mf_ul_prepare_emulation_response,_Bool,"uint8_t*, uint16_t, uint8_t*, uint16_t*, uint32_t*, void*" diff --git a/firmware/targets/f7/furi_hal/furi_hal_nfc.c b/firmware/targets/f7/furi_hal/furi_hal_nfc.c index 8910d887b..ed04a206f 100644 --- a/firmware/targets/f7/furi_hal/furi_hal_nfc.c +++ b/firmware/targets/f7/furi_hal/furi_hal_nfc.c @@ -134,21 +134,36 @@ bool furi_hal_nfc_detect(FuriHalNfcDevData* nfc_data, uint32_t timeout) { if(detected) { if(dev_list[0].type == RFAL_NFC_LISTEN_TYPE_NFCA) { nfc_data->type = FuriHalNfcTypeA; - nfc_data->atqa[0] = dev_list[0].dev.nfca.sensRes.anticollisionInfo; - nfc_data->atqa[1] = dev_list[0].dev.nfca.sensRes.platformInfo; - nfc_data->sak = dev_list[0].dev.nfca.selRes.sak; + nfc_data->a_data.atqa[0] = dev_list[0].dev.nfca.sensRes.anticollisionInfo; + nfc_data->a_data.atqa[1] = dev_list[0].dev.nfca.sensRes.platformInfo; + nfc_data->a_data.sak = dev_list[0].dev.nfca.selRes.sak; uint8_t* cuid_start = dev_list[0].nfcid; if(dev_list[0].nfcidLen == 7) { cuid_start = &dev_list[0].nfcid[3]; } - nfc_data->cuid = (cuid_start[0] << 24) | (cuid_start[1] << 16) | (cuid_start[2] << 8) | - (cuid_start[3]); + nfc_data->a_data.cuid = (cuid_start[0] << 24) | (cuid_start[1] << 16) | + (cuid_start[2] << 8) | (cuid_start[3]); } else if( dev_list[0].type == RFAL_NFC_LISTEN_TYPE_NFCB || dev_list[0].type == RFAL_NFC_LISTEN_TYPE_ST25TB) { nfc_data->type = FuriHalNfcTypeB; } else if(dev_list[0].type == RFAL_NFC_LISTEN_TYPE_NFCF) { nfc_data->type = FuriHalNfcTypeF; + furi_assert(dev_list[0].nfcidLen == RFAL_NFCF_NFCID2_LEN); + memcpy( + &nfc_data->f_data.pmm[0], + dev_list[0].dev.nfcf.sensfRes.PAD0, + RFAL_NFCF_SENSF_RES_PAD0_LEN); + memcpy( + &nfc_data->f_data.pmm[RFAL_NFCF_SENSF_RES_PAD0_LEN], + dev_list[0].dev.nfcf.sensfRes.PAD1, + RFAL_NFCF_SENSF_RES_PAD1_LEN); + nfc_data->f_data.pmm[RFAL_NFCF_SENSF_RES_PAD0_LEN + RFAL_NFCF_SENSF_RES_PAD1_LEN] = + dev_list[0].dev.nfcf.sensfRes.MRTIcheck; + nfc_data->f_data.pmm[RFAL_NFCF_SENSF_RES_PAD0_LEN + RFAL_NFCF_SENSF_RES_PAD1_LEN + 1] = + dev_list[0].dev.nfcf.sensfRes.MRTIupdate; + nfc_data->f_data.pmm[RFAL_NFCF_SENSF_RES_PAD0_LEN + RFAL_NFCF_SENSF_RES_PAD1_LEN + 2] = + dev_list[0].dev.nfcf.sensfRes.PAD2; } else if(dev_list[0].type == RFAL_NFC_LISTEN_TYPE_NFCV) { nfc_data->type = FuriHalNfcTypeV; } @@ -371,15 +386,15 @@ void furi_hal_nfc_listen_start(FuriHalNfcDevData* nfc_data) { // Write PT Memory uint8_t pt_memory[15] = {}; memcpy(pt_memory, nfc_data->uid, nfc_data->uid_len); - pt_memory[10] = nfc_data->atqa[0]; - pt_memory[11] = nfc_data->atqa[1]; + pt_memory[10] = nfc_data->a_data.atqa[0]; + pt_memory[11] = nfc_data->a_data.atqa[1]; if(nfc_data->uid_len == 4) { - pt_memory[12] = nfc_data->sak & ~FURI_HAL_NFC_UID_INCOMPLETE; + pt_memory[12] = nfc_data->a_data.sak & ~FURI_HAL_NFC_UID_INCOMPLETE; } else { pt_memory[12] = FURI_HAL_NFC_UID_INCOMPLETE; } - pt_memory[13] = nfc_data->sak & ~FURI_HAL_NFC_UID_INCOMPLETE; - pt_memory[14] = nfc_data->sak & ~FURI_HAL_NFC_UID_INCOMPLETE; + pt_memory[13] = nfc_data->a_data.sak & ~FURI_HAL_NFC_UID_INCOMPLETE; + pt_memory[14] = nfc_data->a_data.sak & ~FURI_HAL_NFC_UID_INCOMPLETE; st25r3916WritePTMem(pt_memory, sizeof(pt_memory)); // Go to sense diff --git a/firmware/targets/f7/furi_hal/furi_hal_nfc.h b/firmware/targets/f7/furi_hal/furi_hal_nfc.h index dc3f873f3..0d3a043fa 100644 --- a/firmware/targets/f7/furi_hal/furi_hal_nfc.h +++ b/firmware/targets/f7/furi_hal/furi_hal_nfc.h @@ -69,14 +69,25 @@ typedef enum { FuriHalNfcInterfaceNfcDep, } FuriHalNfcInterface; +typedef struct { + uint32_t cuid; + uint8_t atqa[2]; + uint8_t sak; +} FuriHalNfcADevData; + +typedef struct { + uint8_t pmm[8]; +} FuriHalNfcFDevData; + typedef struct { FuriHalNfcType type; FuriHalNfcInterface interface; uint8_t uid_len; uint8_t uid[10]; - uint32_t cuid; - uint8_t atqa[2]; - uint8_t sak; + union { + FuriHalNfcADevData a_data; + FuriHalNfcFDevData f_data; + }; } FuriHalNfcDevData; typedef void ( diff --git a/lib/nfc/helpers/nfc_generators.c b/lib/nfc/helpers/nfc_generators.c index 50c89aba8..90bfbec9d 100644 --- a/lib/nfc/helpers/nfc_generators.c +++ b/lib/nfc/helpers/nfc_generators.c @@ -83,9 +83,9 @@ static void nfc_generate_mf_ul_common(NfcDeviceData* data) { data->nfc_data.interface = FuriHalNfcInterfaceRf; data->nfc_data.uid_len = 7; nfc_generate_mf_ul_uid(data->nfc_data.uid); - data->nfc_data.atqa[0] = 0x44; - data->nfc_data.atqa[1] = 0x00; - data->nfc_data.sak = 0x00; + data->nfc_data.a_data.atqa[0] = 0x44; + data->nfc_data.a_data.atqa[1] = 0x00; + data->nfc_data.a_data.sak = 0x00; data->protocol = NfcDeviceProtocolMifareUl; } @@ -94,9 +94,15 @@ static void data->nfc_data.type = FuriHalNfcTypeA; data->nfc_data.interface = FuriHalNfcInterfaceRf; data->nfc_data.uid_len = uid_len; - data->nfc_data.atqa[0] = 0x44; - data->nfc_data.atqa[1] = 0x00; - data->nfc_data.sak = 0x08; + data->nfc_data.a_data.atqa[0] = 0x44; + data->nfc_data.a_data.atqa[1] = 0x00; + data->nfc_data.a_data.sak = 0x08; + nfc_generate_mf_classic_block_0( + data->mf_classic_data.block[0].value, + uid_len, + data->nfc_data.a_data.sak, + data->nfc_data.a_data.atqa[0], + data->nfc_data.a_data.atqa[1]); data->protocol = NfcDeviceProtocolMifareClassic; data->mf_classic_data.type = type; } @@ -250,9 +256,9 @@ static void mful->data_size = num_pages * 4; mful->data_read = mful->data_size; memcpy(mful->data, data->nfc_data.uid, data->nfc_data.uid_len); - mful->data[7] = data->nfc_data.sak; - mful->data[8] = data->nfc_data.atqa[0]; - mful->data[9] = data->nfc_data.atqa[1]; + mful->data[7] = data->nfc_data.a_data.sak; + mful->data[8] = data->nfc_data.a_data.atqa[0]; + mful->data[9] = data->nfc_data.a_data.atqa[1]; uint16_t config_register_page; uint16_t session_register_page; @@ -358,7 +364,7 @@ void nfc_generate_mf_classic(NfcDeviceData* data, uint8_t uid_len, MfClassicType mf_classic_set_block_read(mfc, i, &mfc->block[i]); } // Set SAK to 18 - data->nfc_data.sak = 0x18; + data->nfc_data.a_data.sak = 0x18; } else if(type == MfClassicType1k) { // Set every block to 0xFF for(uint16_t i = 1; i < MF_CLASSIC_1K_TOTAL_SECTORS_NUM * 4; i += 1) { @@ -370,7 +376,7 @@ void nfc_generate_mf_classic(NfcDeviceData* data, uint8_t uid_len, MfClassicType mf_classic_set_block_read(mfc, i, &mfc->block[i]); } // Set SAK to 08 - data->nfc_data.sak = 0x08; + data->nfc_data.a_data.sak = 0x08; } else if(type == MfClassicTypeMini) { // Set every block to 0xFF for(uint16_t i = 1; i < MF_MINI_TOTAL_SECTORS_NUM * 4; i += 1) { @@ -382,15 +388,15 @@ void nfc_generate_mf_classic(NfcDeviceData* data, uint8_t uid_len, MfClassicType mf_classic_set_block_read(mfc, i, &mfc->block[i]); } // Set SAK to 09 - data->nfc_data.sak = 0x09; + data->nfc_data.a_data.sak = 0x09; } nfc_generate_mf_classic_block_0( data->mf_classic_data.block[0].value, uid_len, - data->nfc_data.sak, - data->nfc_data.atqa[0], - data->nfc_data.atqa[1]); + data->nfc_data.a_data.sak, + data->nfc_data.a_data.atqa[0], + data->nfc_data.a_data.atqa[1]); mfc->type = type; } diff --git a/lib/nfc/helpers/reader_analyzer.c b/lib/nfc/helpers/reader_analyzer.c index 0063b13e7..11ed26a1c 100644 --- a/lib/nfc/helpers/reader_analyzer.c +++ b/lib/nfc/helpers/reader_analyzer.c @@ -41,13 +41,11 @@ struct ReaderAnalyzer { static FuriHalNfcDevData reader_analyzer_nfc_data[] = { //XXX [ReaderAnalyzerNfcDataMfClassic] = - {.sak = 0x08, - .atqa = {0x44, 0x00}, - .interface = FuriHalNfcInterfaceRf, + {.interface = FuriHalNfcInterfaceRf, .type = FuriHalNfcTypeA, .uid_len = 7, .uid = {0x04, 0x77, 0x70, 0x2A, 0x23, 0x4F, 0x80}, - .cuid = 0x2A234F80}, + .a_data = {.sak = 0x08, .atqa = {0x44, 0x00}, .cuid = 0x2A234F80}}, }; void reader_analyzer_parse(ReaderAnalyzer* instance, uint8_t* buffer, size_t size) { @@ -100,7 +98,7 @@ int32_t reader_analyzer_thread(void* context) { ReaderAnalyzer* reader_analyzer_alloc() { ReaderAnalyzer* instance = malloc(sizeof(ReaderAnalyzer)); - reader_analyzer_nfc_data[ReaderAnalyzerNfcDataMfClassic].cuid = rand(); //XXX + reader_analyzer_nfc_data[ReaderAnalyzerNfcDataMfClassic].a_data.cuid = rand(); //XXX furi_hal_random_fill_buf( (uint8_t*)&reader_analyzer_nfc_data[ReaderAnalyzerNfcDataMfClassic].uid, 7); instance->nfc_data = reader_analyzer_nfc_data[ReaderAnalyzerNfcDataMfClassic]; @@ -134,7 +132,7 @@ void reader_analyzer_start(ReaderAnalyzer* instance, ReaderAnalyzerMode mode) { instance->debug_log = nfc_debug_log_alloc(); } if(mode & ReaderAnalyzerModeMfkey) { - instance->mfkey32 = mfkey32_alloc(instance->nfc_data.cuid); + instance->mfkey32 = mfkey32_alloc(instance->nfc_data.a_data.cuid); if(instance->mfkey32) { mfkey32_set_callback(instance->mfkey32, reader_analyzer_mfkey_callback, instance); } diff --git a/lib/nfc/nfc_device.c b/lib/nfc/nfc_device.c index f07db42cd..af0ef42ae 100644 --- a/lib/nfc/nfc_device.c +++ b/lib/nfc/nfc_device.c @@ -1410,9 +1410,9 @@ bool nfc_device_save(NfcDevice* dev, const char* dev_name) { // Write ATQA, SAK if(!flipper_format_write_comment_cstr(file, "ISO14443 specific fields")) break; // Save ATQA in MSB order for correct companion apps display - uint8_t atqa[2] = {data->atqa[1], data->atqa[0]}; + uint8_t atqa[2] = {data->a_data.atqa[1], data->a_data.atqa[0]}; if(!flipper_format_write_hex(file, "ATQA", atqa, 2)) break; - if(!flipper_format_write_hex(file, "SAK", &data->sak, 1)) break; + if(!flipper_format_write_hex(file, "SAK", &data->a_data.sak, 1)) break; } // Save more data if necessary @@ -1503,22 +1503,22 @@ static bool nfc_device_load_data(NfcDevice* dev, FuriString* path, bool show_dia if(!flipper_format_read_hex(file, "UID", data->uid, data->uid_len)) break; if(dev->format != NfcDeviceSaveFormatNfcV) { if(version == version_with_lsb_atqa) { - if(!flipper_format_read_hex(file, "ATQA", data->atqa, 2)) break; + if(!flipper_format_read_hex(file, "ATQA", data->a_data.atqa, 2)) break; } else { uint8_t atqa[2] = {}; if(!flipper_format_read_hex(file, "ATQA", atqa, 2)) break; - data->atqa[0] = atqa[1]; - data->atqa[1] = atqa[0]; + data->a_data.atqa[0] = atqa[1]; + data->a_data.atqa[1] = atqa[0]; } - if(!flipper_format_read_hex(file, "SAK", &data->sak, 1)) break; + if(!flipper_format_read_hex(file, "SAK", &data->a_data.sak, 1)) break; } // Load CUID uint8_t* cuid_start = data->uid; if(data->uid_len == 7) { cuid_start = &data->uid[3]; } - data->cuid = (cuid_start[0] << 24) | (cuid_start[1] << 16) | (cuid_start[2] << 8) | - (cuid_start[3]); + data->a_data.cuid = (cuid_start[0] << 24) | (cuid_start[1] << 16) | (cuid_start[2] << 8) | + (cuid_start[3]); // Parse other data if(dev->format == NfcDeviceSaveFormatMifareUl) { if(!nfc_device_load_mifare_ul_data(file, dev)) break; @@ -1614,6 +1614,8 @@ void nfc_device_data_clear(NfcDeviceData* dev_data) { mf_ul_reset(&dev_data->mf_ul_data); } else if(dev_data->protocol == NfcDeviceProtocolEMV) { memset(&dev_data->emv_data, 0, sizeof(EmvData)); + } else if(dev_data->protocol == NfcDeviceProtocolFelica) { + felica_clear(&dev_data->felica_data); } memset(&dev_data->nfc_data, 0, sizeof(FuriHalNfcDevData)); dev_data->protocol = NfcDeviceProtocolUnknown; diff --git a/lib/nfc/nfc_device.h b/lib/nfc/nfc_device.h index d5a9e57fb..1d66ced68 100644 --- a/lib/nfc/nfc_device.h +++ b/lib/nfc/nfc_device.h @@ -12,6 +12,8 @@ #include #include #include +#include +#include #ifdef __cplusplus extern "C" { @@ -32,7 +34,8 @@ typedef enum { NfcDeviceProtocolMifareUl, NfcDeviceProtocolMifareClassic, NfcDeviceProtocolMifareDesfire, - NfcDeviceProtocolNfcV + NfcDeviceProtocolNfcV, + NfcDeviceProtocolFelica, } NfcProtocol; typedef enum { @@ -42,6 +45,7 @@ typedef enum { NfcDeviceSaveFormatMifareClassic, NfcDeviceSaveFormatMifareDesfire, NfcDeviceSaveFormatNfcV, + NfcDeviceSaveFormatFelica, } NfcDeviceSaveFormat; typedef struct { @@ -61,6 +65,7 @@ typedef enum { NfcReadModeMfDesfire, NfcReadModeEMV, NfcReadModeNFCA, + NfcReadModeFelica, } NfcReadMode; typedef struct { @@ -78,6 +83,7 @@ typedef struct { MfClassicData mf_classic_data; MifareDesfireData mf_df_data; NfcVData nfcv_data; + FelicaData felica_data; }; FuriString* parsed_data; } NfcDeviceData; diff --git a/lib/nfc/nfc_types.c b/lib/nfc/nfc_types.c index 02ca85580..6a256985c 100644 --- a/lib/nfc/nfc_types.c +++ b/lib/nfc/nfc_types.c @@ -65,3 +65,63 @@ const char* nfc_mf_classic_type(MfClassicType type) { return "Mifare Classic"; } } + +const char* nfc_felica_type(FelicaICType type) { + if(type == FelicaICType576B) { + return "FeliCa Classic 576B"; + } else if(type == FelicaICType2K) { + return "FeliCa Classic 2K"; + } else if(type == FelicaICType4K) { + return "FeliCa Classic 4K"; + } else if(type == FelicaICTypeFRAM_4K) { + return "FeliCa Classic 4K (FRAM)"; + } else if(type == FelicaICTypeFRAM_9K) { + return "FeliCa Classic 9K"; + } else if(type == FelicaICTypeEMV_16K) { + return "FeliCa Classic EMV 16K"; + } else if(type == FelicaICTypeEMV_32K) { + return "FeliCa Classic EMV 32K"; + } else if(type == FelicaICTypeEMV_36K) { + return "FeliCa Classic EMV 36K"; + } else if(type == FelicaICTypeEMV_36K) { + return "FeliCa Classic EMV 36K"; + } else if(type == FelicaICTypeSD1WithDES) { + return "FeliCa SD1 (DES compatible)"; + } else if(type == FelicaICTypeSD1) { + return "FeliCa SD1"; + } else if(type == FelicaICTypeRC_SA08) { + return "FeliCa RC-SA08"; + } else if(type == FelicaICTypeSD2WithDES) { + return "FeliCa SD2 (DES compatible)"; + } else if(type == FelicaICTypeSD2_4K) { + return "FeliCa SD2 4K"; + } else if(type == FelicaICTypeSD2_6K) { + return "FeliCa SD2 6K"; + } else if(type == FelicaICTypeRC_SA24_6K) { + return "FeliCa RC-SA24 6K"; + } else if(type == FelicaICTypeRC_SA24_10K) { + return "FeliCa RC-SA24 6K"; + } else if(type == FelicaICTypeMobileIC_V1) { + return "Mobile FeliCa v1"; + } else if(type == FelicaICTypeMobileIC_V2) { + return "Mobile FeliCa v2"; + } else if(type == FelicaICTypeMobileIC_V3) { + return "Mobile FeliCa v3"; + } else if(type == FelicaICTypeMobileIC_V4) { + return "Mobile FeliCa v4"; + } else if(type == FelicaICTypeMobileIC_V4_1) { + return "Mobile FeliCa v4.1"; + } else if(type == FelicaICTypeLite) { + return "FeliCa Lite"; + } else if(type == FelicaICTypeLiteS) { + return "FeliCa Lite-S"; + } else if(type == FelicaICTypeLink) { + return "FeliCa Link"; + } else if(type == FelicaICTypePlug) { + return "FeliCa Plug"; + } else if(type == FelicaICTypeSuica) { + return "FeliCa (SuiCa)"; + } else { + return "FeliCa"; + } +} diff --git a/lib/nfc/nfc_types.h b/lib/nfc/nfc_types.h index fb53ce7c2..9d0bab1d7 100644 --- a/lib/nfc/nfc_types.h +++ b/lib/nfc/nfc_types.h @@ -9,3 +9,5 @@ const char* nfc_guess_protocol(NfcProtocol protocol); const char* nfc_mf_ul_type(MfUltralightType type, bool full_name); const char* nfc_mf_classic_type(MfClassicType type); + +const char* nfc_felica_type(FelicaICType type); \ No newline at end of file diff --git a/lib/nfc/nfc_worker.c b/lib/nfc/nfc_worker.c index afa474222..0715f528f 100644 --- a/lib/nfc/nfc_worker.c +++ b/lib/nfc/nfc_worker.c @@ -535,20 +535,20 @@ static bool nfc_worker_read_bank_card(NfcWorker* nfc_worker, FuriHalNfcTxRxConte static bool nfc_worker_read_nfca(NfcWorker* nfc_worker, FuriHalNfcTxRxContext* tx_rx) { FuriHalNfcDevData* nfc_data = &nfc_worker->dev_data->nfc_data; + FuriHalNfcADevData* a_data = &nfc_data->a_data; bool card_read = false; furi_hal_nfc_sleep(); - if(mf_ul_check_card_type(nfc_data->atqa[0], nfc_data->atqa[1], nfc_data->sak)) { + if(mf_ul_check_card_type(a_data)) { FURI_LOG_I(TAG, "Mifare Ultralight / NTAG detected"); nfc_worker->dev_data->protocol = NfcDeviceProtocolMifareUl; card_read = nfc_worker_read_mf_ultralight(nfc_worker, tx_rx); - } else if(mf_classic_check_card_type(nfc_data->atqa[0], nfc_data->atqa[1], nfc_data->sak)) { + } else if(mf_classic_check_card_type(a_data)) { FURI_LOG_I(TAG, "Mifare Classic detected"); nfc_worker->dev_data->protocol = NfcDeviceProtocolMifareClassic; - nfc_worker->dev_data->mf_classic_data.type = - mf_classic_get_classic_type(nfc_data->atqa[0], nfc_data->atqa[1], nfc_data->sak); + nfc_worker->dev_data->mf_classic_data.type = mf_classic_get_classic_type(a_data); card_read = nfc_worker_read_mf_classic(nfc_worker, tx_rx); - } else if(mf_df_check_card_type(nfc_data->atqa[0], nfc_data->atqa[1], nfc_data->sak)) { + } else if(mf_df_check_card_type(a_data)) { FURI_LOG_I(TAG, "Mifare DESFire detected"); nfc_worker->dev_data->protocol = NfcDeviceProtocolMifareDesfire; if(!nfc_worker_read_mf_desfire(nfc_worker, tx_rx)) { @@ -572,6 +572,46 @@ static bool nfc_worker_read_nfca(NfcWorker* nfc_worker, FuriHalNfcTxRxContext* t return card_read; } +static bool nfc_worker_read_felica(NfcWorker* nfc_worker, FuriHalNfcTxRxContext* tx_rx) { + bool read_success = false; + FuriHalNfcDevData* nfc_data = &nfc_worker->dev_data->nfc_data; + FelicaData* data = &nfc_worker->dev_data->felica_data; + + if(furi_hal_rtc_is_flag_set(FuriHalRtcFlagDebug)) { + reader_analyzer_prepare_tx_rx(nfc_worker->reader_analyzer, tx_rx, false); + reader_analyzer_start(nfc_worker->reader_analyzer, ReaderAnalyzerModeDebugLog); + } + + do { + if(!furi_hal_nfc_detect(&nfc_worker->dev_data->nfc_data, 300)) break; + if(!felica_read_card(tx_rx, data, nfc_data->uid, nfc_data->f_data.pmm)) break; + read_success = true; + } while(false); + + if(furi_hal_rtc_is_flag_set(FuriHalRtcFlagDebug)) { + reader_analyzer_stop(nfc_worker->reader_analyzer); + } + + return read_success; +} + +static bool nfc_worker_read_nfcf(NfcWorker* nfc_worker, FuriHalNfcTxRxContext* tx_rx) { + FuriHalNfcDevData* nfc_data = &nfc_worker->dev_data->nfc_data; + FuriHalNfcFDevData* f_data = &nfc_data->f_data; + + bool card_read = false; + furi_hal_nfc_sleep(); + if(felica_check_ic_type(f_data->pmm)) { + FURI_LOG_I(TAG, "FeliCa detected"); + nfc_worker->dev_data->protocol = NfcDeviceProtocolFelica; + nfc_worker->dev_data->felica_data.type = felica_get_ic_type(f_data->pmm); + card_read = nfc_worker_read_felica(nfc_worker, tx_rx); + } else { + nfc_worker->dev_data->protocol = NfcDeviceProtocolUnknown; + } + return card_read; +} + void nfc_worker_read(NfcWorker* nfc_worker) { furi_assert(nfc_worker); furi_assert(nfc_worker->callback); @@ -616,6 +656,11 @@ void nfc_worker_read(NfcWorker* nfc_worker) { event = NfcWorkerEventReadUidNfcB; break; } else if(nfc_data->type == FuriHalNfcTypeF) { + nfc_worker->callback(NfcWorkerEventCardDetected, nfc_worker->context); + if(nfc_worker_read_nfcf(nfc_worker, &tx_rx)) { + event = NfcWorkerEventReadFelica; + break; + } event = NfcWorkerEventReadUidNfcF; break; } else if(nfc_data->type == FuriHalNfcTypeV) { @@ -664,8 +709,8 @@ void nfc_worker_read_type(NfcWorker* nfc_worker) { if(nfc_data->type == FuriHalNfcTypeA) { if(read_mode == NfcReadModeMfClassic) { nfc_worker->dev_data->protocol = NfcDeviceProtocolMifareClassic; - nfc_worker->dev_data->mf_classic_data.type = mf_classic_get_classic_type( - nfc_data->atqa[0], nfc_data->atqa[1], nfc_data->sak); + nfc_worker->dev_data->mf_classic_data.type = + mf_classic_get_classic_type(&nfc_data->a_data); if(nfc_worker_read_mf_classic(nfc_worker, &tx_rx)) { FURI_LOG_D(TAG, "Card read"); dev_data->protocol = NfcDeviceProtocolMifareClassic; @@ -701,6 +746,17 @@ void nfc_worker_read_type(NfcWorker* nfc_worker) { event = NfcWorkerEventReadUidNfcA; break; } + } else if(nfc_data->type == FuriHalNfcTypeF) { + if(read_mode == NfcReadModeFelica) { + nfc_worker->dev_data->protocol = NfcDeviceProtocolFelica; + if(nfc_worker_read_felica(nfc_worker, &tx_rx)) { + nfc_worker->dev_data->protocol = NfcDeviceProtocolFelica; + if(nfc_worker_read_felica(nfc_worker, &tx_rx)) { + event = NfcWorkerEventReadFelica; + break; + } + } + } } } else { if(!card_not_detected_notified) { @@ -726,7 +782,8 @@ void nfc_worker_emulate_uid(NfcWorker* nfc_worker) { // Need to save ATS to support ISO-14443A-4 emulation while(nfc_worker->state == NfcWorkerStateUidEmulate) { - if(furi_hal_nfc_listen(data->uid, data->uid_len, data->atqa, data->sak, false, 100)) { + if(furi_hal_nfc_listen( + data->uid, data->uid_len, data->a_data.atqa, data->a_data.sak, false, 100)) { if(furi_hal_nfc_tx_rx(&tx_rx, 100)) { reader_data->size = tx_rx.rx_bits / 8; if(reader_data->size > 0) { @@ -747,8 +804,11 @@ void nfc_worker_emulate_apdu(NfcWorker* nfc_worker) { FuriHalNfcDevData params = { .uid = {0xCF, 0x72, 0xd4, 0x40}, .uid_len = 4, - .atqa = {0x00, 0x04}, - .sak = 0x20, + .a_data = + { + .atqa = {0x00, 0x04}, + .sak = 0x20, + }, .type = FuriHalNfcTypeA, }; @@ -758,7 +818,8 @@ void nfc_worker_emulate_apdu(NfcWorker* nfc_worker) { } while(nfc_worker->state == NfcWorkerStateEmulateApdu) { //-V1044 - if(furi_hal_nfc_listen(params.uid, params.uid_len, params.atqa, params.sak, false, 300)) { + if(furi_hal_nfc_listen( + params.uid, params.uid_len, params.a_data.atqa, params.a_data.sak, false, 300)) { FURI_LOG_D(TAG, "POS terminal detected"); if(emv_card_emulation(&tx_rx)) { FURI_LOG_D(TAG, "EMV card emulated"); @@ -799,8 +860,8 @@ void nfc_worker_emulate_mf_ultralight(NfcWorker* nfc_worker) { furi_hal_nfc_emulate_nfca( nfc_data->uid, nfc_data->uid_len, - nfc_data->atqa, - nfc_data->sak, + nfc_data->a_data.atqa, + nfc_data->a_data.sak, mf_ul_prepare_emulation_response, &emulator, 5000); @@ -1114,8 +1175,7 @@ void nfc_worker_write_mf_classic(NfcWorker* nfc_worker) { } FURI_LOG_I(TAG, "Check mf classic type"); - MfClassicType type = - mf_classic_get_classic_type(nfc_data.atqa[0], nfc_data.atqa[1], nfc_data.sak); + MfClassicType type = mf_classic_get_classic_type(&nfc_data.a_data); if(type != nfc_worker->dev_data->mf_classic_data.type) { FURI_LOG_E(TAG, "Wrong mf classic type"); nfc_worker->callback(NfcWorkerEventWrongCard, nfc_worker->context); @@ -1187,8 +1247,7 @@ void nfc_worker_update_mf_classic(NfcWorker* nfc_worker) { } FURI_LOG_I(TAG, "Check MF classic type"); - MfClassicType type = - mf_classic_get_classic_type(nfc_data.atqa[0], nfc_data.atqa[1], nfc_data.sak); + MfClassicType type = mf_classic_get_classic_type(&nfc_data.a_data); if(type != nfc_worker->dev_data->mf_classic_data.type) { FURI_LOG_E(TAG, "MF classic type mismatch"); nfc_worker->callback(NfcWorkerEventWrongCard, nfc_worker->context); @@ -1250,7 +1309,7 @@ void nfc_worker_mf_ultralight_read_auth(NfcWorker* nfc_worker) { while(nfc_worker->state == NfcWorkerStateReadMfUltralightReadAuth) { furi_hal_nfc_sleep(); if(furi_hal_nfc_detect(nfc_data, 300) && nfc_data->type == FuriHalNfcTypeA) { - if(mf_ul_check_card_type(nfc_data->atqa[0], nfc_data->atqa[1], nfc_data->sak)) { + if(mf_ul_check_card_type(&nfc_data->a_data)) { nfc_worker->callback(NfcWorkerEventCardDetected, nfc_worker->context); if(data->auth_method == MfUltralightAuthMethodManual || data->auth_method == MfUltralightAuthMethodAuto) { diff --git a/lib/nfc/nfc_worker.h b/lib/nfc/nfc_worker.h index 0613fbf0a..89ce91186 100644 --- a/lib/nfc/nfc_worker.h +++ b/lib/nfc/nfc_worker.h @@ -38,6 +38,7 @@ typedef enum { NfcWorkerEventReadUidNfcV, NfcWorkerEventReadUidNfcF, NfcWorkerEventReadUidNfcA, + NfcWorkerEventReadFelica, NfcWorkerEventReadMfUltralight, NfcWorkerEventReadMfDesfire, NfcWorkerEventReadMfClassicDone, diff --git a/lib/nfc/nfc_worker_i.h b/lib/nfc/nfc_worker_i.h index d13d3c5c8..eda42c313 100644 --- a/lib/nfc/nfc_worker_i.h +++ b/lib/nfc/nfc_worker_i.h @@ -11,6 +11,7 @@ #include #include #include +#include #include #include #include diff --git a/lib/nfc/parsers/plantain_4k_parser.c b/lib/nfc/parsers/plantain_4k_parser.c index 19da0b5eb..3990461fc 100644 --- a/lib/nfc/parsers/plantain_4k_parser.c +++ b/lib/nfc/parsers/plantain_4k_parser.c @@ -70,8 +70,8 @@ bool plantain_4k_parser_read(NfcWorker* nfc_worker, FuriHalNfcTxRxContext* tx_rx furi_assert(nfc_worker); MfClassicReader reader = {}; - FuriHalNfcDevData* nfc_data = &nfc_worker->dev_data->nfc_data; - reader.type = mf_classic_get_classic_type(nfc_data->atqa[0], nfc_data->atqa[1], nfc_data->sak); + FuriHalNfcADevData* nfc_a_data = &nfc_worker->dev_data->nfc_data.a_data; + reader.type = mf_classic_get_classic_type(nfc_a_data); for(size_t i = 0; i < COUNT_OF(plantain_keys_4k); i++) { mf_classic_reader_add_sector( &reader, diff --git a/lib/nfc/parsers/plantain_parser.c b/lib/nfc/parsers/plantain_parser.c index 2e4091dda..758b038e8 100644 --- a/lib/nfc/parsers/plantain_parser.c +++ b/lib/nfc/parsers/plantain_parser.c @@ -45,8 +45,8 @@ bool plantain_parser_read(NfcWorker* nfc_worker, FuriHalNfcTxRxContext* tx_rx) { furi_assert(nfc_worker); MfClassicReader reader = {}; - FuriHalNfcDevData* nfc_data = &nfc_worker->dev_data->nfc_data; - reader.type = mf_classic_get_classic_type(nfc_data->atqa[0], nfc_data->atqa[1], nfc_data->sak); + FuriHalNfcADevData* nfc_a_data = &nfc_worker->dev_data->nfc_data.a_data; + reader.type = mf_classic_get_classic_type(nfc_a_data); for(size_t i = 0; i < COUNT_OF(plantain_keys); i++) { mf_classic_reader_add_sector( &reader, plantain_keys[i].sector, plantain_keys[i].key_a, plantain_keys[i].key_b); diff --git a/lib/nfc/parsers/troika_4k_parser.c b/lib/nfc/parsers/troika_4k_parser.c index 1f1b85a5c..aca0ee2c0 100644 --- a/lib/nfc/parsers/troika_4k_parser.c +++ b/lib/nfc/parsers/troika_4k_parser.c @@ -67,8 +67,8 @@ bool troika_4k_parser_read(NfcWorker* nfc_worker, FuriHalNfcTxRxContext* tx_rx) furi_assert(nfc_worker); MfClassicReader reader = {}; - FuriHalNfcDevData* nfc_data = &nfc_worker->dev_data->nfc_data; - reader.type = mf_classic_get_classic_type(nfc_data->atqa[0], nfc_data->atqa[1], nfc_data->sak); + FuriHalNfcADevData* nfc_a_data = &nfc_worker->dev_data->nfc_data.a_data; + reader.type = mf_classic_get_classic_type(nfc_a_data); for(size_t i = 0; i < COUNT_OF(troika_4k_keys); i++) { mf_classic_reader_add_sector( &reader, troika_4k_keys[i].sector, troika_4k_keys[i].key_a, troika_4k_keys[i].key_b); diff --git a/lib/nfc/parsers/troika_parser.c b/lib/nfc/parsers/troika_parser.c index bfd22364b..fe699a765 100644 --- a/lib/nfc/parsers/troika_parser.c +++ b/lib/nfc/parsers/troika_parser.c @@ -43,8 +43,8 @@ bool troika_parser_read(NfcWorker* nfc_worker, FuriHalNfcTxRxContext* tx_rx) { furi_assert(nfc_worker); MfClassicReader reader = {}; - FuriHalNfcDevData* nfc_data = &nfc_worker->dev_data->nfc_data; - reader.type = mf_classic_get_classic_type(nfc_data->atqa[0], nfc_data->atqa[1], nfc_data->sak); + FuriHalNfcADevData* nfc_a_data = &nfc_worker->dev_data->nfc_data.a_data; + reader.type = mf_classic_get_classic_type(nfc_a_data); for(size_t i = 0; i < COUNT_OF(troika_keys); i++) { mf_classic_reader_add_sector( diff --git a/lib/nfc/parsers/two_cities.c b/lib/nfc/parsers/two_cities.c index d6d4279dd..b6a8bc552 100644 --- a/lib/nfc/parsers/two_cities.c +++ b/lib/nfc/parsers/two_cities.c @@ -71,8 +71,8 @@ bool two_cities_parser_read(NfcWorker* nfc_worker, FuriHalNfcTxRxContext* tx_rx) furi_assert(nfc_worker); MfClassicReader reader = {}; - FuriHalNfcDevData* nfc_data = &nfc_worker->dev_data->nfc_data; - reader.type = mf_classic_get_classic_type(nfc_data->atqa[0], nfc_data->atqa[1], nfc_data->sak); + FuriHalNfcADevData* nfc_a_data = &nfc_worker->dev_data->nfc_data.a_data; + reader.type = mf_classic_get_classic_type(nfc_a_data); for(size_t i = 0; i < COUNT_OF(two_cities_keys_4k); i++) { mf_classic_reader_add_sector( &reader, diff --git a/lib/nfc/protocols/felica.c b/lib/nfc/protocols/felica.c new file mode 100644 index 000000000..d9489cb50 --- /dev/null +++ b/lib/nfc/protocols/felica.c @@ -0,0 +1,658 @@ +#include +#include +#include "felica.h" +#include "nfc_util.h" +#include +#include "furi_hal_nfc.h" + +#define TAG "FeliCa" + +bool felica_check_ic_type(uint8_t* PMm) { + uint8_t ic_type = PMm[0]; + uint8_t rom_type = PMm[1]; + + bool is_valid_ic = false; + if(ic_type == 0xff) { // RC-S967 in nfc-dep + is_valid_ic = true; + } else if(ic_type == 0xf0 || ic_type == 0xf2) { // Lite(S) + is_valid_ic = true; + } else if(ic_type == 0xe1) { // RC-S967 in plug mode + is_valid_ic = true; + } else if(ic_type == 0xe0) { // RC-S926 + is_valid_ic = true; + } else if(ic_type >= 0x44 && ic_type <= 0x48) { // SD2 + is_valid_ic = true; + } else if(ic_type == 0x3e && rom_type == 0x03) { // RC-SA08 + return true; + } else if(ic_type == 0x35) { // RC-SA01 + is_valid_ic = true; + } else if(ic_type == 0x32) { // RC-SA00 + is_valid_ic = true; + } else if(ic_type == 0x31) { // Suica/PASMO + is_valid_ic = true; + } else if(ic_type == 0x20) { // RC-S962 + is_valid_ic = true; + } else if(ic_type >= 0x10 && ic_type <= 0x1f) { // Mobile IC version 2/3 + is_valid_ic = true; + } else if(ic_type == 0x0d) { // RC-S960 + is_valid_ic = true; + } else if(ic_type == 0x0c) { // RC-S954 + is_valid_ic = true; + } else if(ic_type == 0x0b) { // Old Suica? + is_valid_ic = true; + } else if(ic_type == 0x09) { // RC-S953 + is_valid_ic = true; + } else if(ic_type == 0x08) { // RC-S952 + is_valid_ic = true; + } else if(ic_type == 0x06 || ic_type == 0x07) { // Mobile IC version 1 + is_valid_ic = true; + } else if(ic_type == 0x02) { // RC-S919 + is_valid_ic = true; + } else if(ic_type == 0x01) { // RC-S915 + is_valid_ic = true; + } else if(ic_type == 0x00) { // RC-S830 + is_valid_ic = true; + } + + if(!is_valid_ic) { + return false; + } + + // need more samples to confirm below + /* + if (rom_type != 0x01) { + return false; + } + */ + + return true; +} + +FelicaICType felica_get_ic_type(uint8_t* PMm) { + uint8_t rom_type = PMm[0]; + uint8_t ic_type = PMm[1]; + + UNUSED(rom_type); + switch(ic_type) { + case 0xff: + return FelicaICTypeLink; + case 0xf2: + return FelicaICTypeLink; + case 0xf1: + return FelicaICTypeLiteS; + case 0xf0: + return FelicaICTypeLite; + case 0xe1: + return FelicaICTypeLink; + case 0xe0: + return FelicaICTypePlug; + case 0x48: + return FelicaICTypeSD2_6K; + case 0x47: + return FelicaICTypeRC_SA24_6K; + case 0x46: + return FelicaICTypeSD2_4K; + case 0x45: + case 0x44: + return FelicaICTypeSD2WithDES; + case 0x3e: + return FelicaICTypeRC_SA08; + case 0x35: + return FelicaICTypeSD1; + case 0x32: + return FelicaICTypeSD1WithDES; + case 0x31: + return FelicaICTypeSuica; + case 0x20: + return FelicaICTypeFRAM_4K; + case 0x1f: + case 0x1e: + case 0x1d: + case 0x1c: + case 0x1b: + case 0x1a: + case 0x19: + case 0x18: + return FelicaICTypeMobileIC_V4_1; + case 0x17: + return FelicaICTypeMobileIC_V4; + case 0x16: + case 0x15: + case 0x14: + return FelicaICTypeMobileIC_V3; + case 0x13: + case 0x12: + case 0x11: + case 0x10: + return FelicaICTypeMobileIC_V2; + case 0x0d: + return FelicaICTypeFRAM_9K; + case 0x0c: + return FelicaICTypeEMV_36K; + case 0x0b: // Old Suica? + return FelicaICTypeSuica; + case 0x09: + return FelicaICTypeEMV_16K; + case 0x08: + return FelicaICTypeEMV_32K; + case 0x07: + case 0x06: + return FelicaICTypeMobileIC_V1; + case 0x02: + return FelicaICType576B; + case 0x01: + return FelicaICType4K; + case 0x00: + return FelicaICType2K; + } + + return FelicaICType2K; +} + +uint8_t felica_prepare_unencrypted_read( + uint8_t* dest, + const FelicaReader* reader, + const uint16_t* service_code_list, + uint8_t service_count, + const uint32_t* block_list, + uint8_t block_count) { + dest[0] = FELICA_UNENCRYPTED_READ_CMD; + memcpy(&dest[1], reader->current_idm, 8); + + dest[9] = service_count; + uint8_t msg_len = 10; + for(int i = 0; i < service_count; i++) { + uint16_t service_code = service_code_list[i]; + dest[msg_len++] = service_code & 0xFF; + dest[msg_len++] = service_code >> 8; + } + + dest[msg_len++] = block_count; + for(int i = 0; i < block_count; i++) { + uint16_t block_num = block_list[i]; + dest[msg_len++] = block_num & 0xFF; + dest[msg_len++] = block_num >> 8; + } + + return msg_len; +} + +uint8_t felica_lite_prepare_unencrypted_read( + uint8_t* dest, + const FelicaReader* reader, + bool is_read_only, + const uint8_t* block_list, + uint8_t block_count) { + dest[0] = FELICA_UNENCRYPTED_READ_CMD; + memcpy(&dest[1], reader->current_idm, 8); + + dest[9] = 1; + uint8_t msg_len = 10; + uint8_t service_code = + FelicaServiceTypeRandom | + ((is_read_only) ? FelicaServiceAttributeUnauthRO : FelicaServiceAttributeUnauthRO); + + dest[msg_len++] = service_code & 0xFF; + dest[msg_len++] = service_code >> 8; + + dest[msg_len++] = block_count; + for(int i = 0; i < block_count; i++) { + dest[msg_len++] = IS_2_BYTE_BLOCK_LIST_ELEMENT; + dest[msg_len++] = block_list[i]; + } + + return msg_len; +} + +uint16_t felica_parse_unencrypted_read( + uint8_t* buf, + uint8_t len, + FelicaReader* reader, + uint8_t* out, + uint16_t out_len) { + if(len < 12) { + return false; + } + len--; + buf++; + + if(*buf != FELICA_UNENCRYPTED_READ_RES) { + return false; + } + len--; + buf++; + + if(memcmp(buf, reader->current_idm, 8) != 0) { + return false; + } + len -= 8; + buf += 8; + + reader->status_flags[0] = buf[0]; + reader->status_flags[1] = buf[1]; + len -= 2; + buf += 2; + if(reader->status_flags[0] != 0) { + FURI_LOG_W(TAG, "SF1: %02X SF2: %02X", reader->status_flags[0], reader->status_flags[1]); + return 0; + } + + if(len < 1) { + return 0; + } + uint16_t data_length = *buf * FELICA_BLOCK_SIZE; + len--; + buf++; + + if(len < data_length || out_len < data_length) { + return 0; + } + + memcpy(out, buf, data_length); + + return data_length; +} + +uint8_t felica_prepare_unencrypted_write( + uint8_t* dest, + FelicaReader* reader, + const uint16_t* service_code_list, + uint8_t service_count, + const uint32_t* block_list, + uint8_t block_count, + const uint8_t* block_data) { + dest[0] = FELICA_UNENCRYPTED_WRITE_CMD; + memcpy(&dest[1], reader->current_idm, 8); + + dest[9] = service_count; + uint8_t msg_len = 10; + for(int i = 0; i < service_count; i++) { + uint16_t service_code = service_code_list[i]; + dest[msg_len++] = service_code & 0xFF; + dest[msg_len++] = service_code >> 8; + } + + dest[msg_len++] = block_count; + for(int i = 0; i < block_count; i++) { + uint16_t block_num = block_list[i]; + dest[msg_len++] = block_num & 0xFF; + dest[msg_len++] = block_num >> 8; + } + + uint16_t data_length = block_count * FELICA_BLOCK_SIZE; + memcpy(dest + msg_len, block_data, data_length); + msg_len += data_length; + return msg_len; +} + +uint8_t felica_lite_prepare_unencrypted_write( + uint8_t* dest, + const FelicaReader* reader, + const uint8_t* block_list, + uint8_t block_count, + const uint8_t* block_data) { + dest[0] = FELICA_UNENCRYPTED_WRITE_CMD; + memcpy(&dest[1], reader->current_idm, 8); + + dest[9] = 1; + uint8_t msg_len = 10; + uint8_t service_code = FelicaServiceTypeRandom | FelicaServiceAttributeUnauthRW; + dest[msg_len++] = service_code & 0xFF; + dest[msg_len++] = service_code >> 8; + + dest[msg_len++] = block_count; + for(int i = 0; i < block_count; i++) { + dest[msg_len++] = block_list[i]; + dest[msg_len++] = IS_2_BYTE_BLOCK_LIST_ELEMENT; + } + + uint16_t data_length = block_count * FELICA_BLOCK_SIZE; + memcpy(dest + msg_len, block_data, data_length); + msg_len += data_length; + return msg_len; +} + +bool felica_parse_unencrypted_write(uint8_t* buf, uint8_t len, FelicaReader* reader) { + if(len < 12) { + return false; + } + len--; + buf++; + + if(*buf != FELICA_UNENCRYPTED_WRITE_RES) { + return false; + } + len--; + buf++; + + if(memcmp(buf, reader->current_idm, 8) != 0) { + return false; + } + len -= 8; + buf += 8; + + reader->status_flags[0] = buf[0]; + reader->status_flags[1] = buf[1]; + len -= 2; + buf += 2; + if(reader->status_flags[0] != 0) { + FURI_LOG_W(TAG, "SF1: %02X SF2: %02X", reader->status_flags[0], reader->status_flags[1]); + return 0; + } + + return true; +} + +void felica_parse_system_info(FelicaSystem* system, uint8_t* IDm, uint8_t* PMm) { + memcpy(system->idm, IDm, 8); + memcpy(system->pmm, PMm, 8); + for(int i = 0; i < 6; i++) { + char MRT_byte = PMm[2 + i]; + FelicaMRTParts* mrt_data = &system->maximum_response_times[i]; + mrt_data->real_a = (MRT_byte & 7) + 1; + MRT_byte >>= 3; + mrt_data->real_b = (MRT_byte & 7) + 1; + MRT_byte >>= 3; + mrt_data->exponent = (MRT_byte & 3); + } +} + +bool felica_lite_can_read_without_mac(uint8_t* mc_r_restr, uint8_t block_number) { + if(block_number > REG_LITE_BLOCK) { + return true; + } + uint8_t byte = mc_r_restr[block_number < 8 ? 0 : 1]; + return ((byte >> (block_number % 8)) & 1) == 0; +} + +void felica_define_normal_block(FelicaService* service, uint16_t number, uint8_t* data) { + FelicaBlock* block = malloc(sizeof(FelicaBlock)); + memcpy(block->data, data, FELICA_BLOCK_SIZE); + FelicaBlockList_set_at(service->blocks, number, block); +} + +bool felica_read_lite_system( + FuriHalNfcTxRxContext* tx_rx, + FelicaReader* reader, + FelicaData* data, + FelicaSystem* system) { + const uint8_t fixed_blocks[] = { + SYS_CODE_LITE_BLOCK, + DEVICE_ID_LITE_BLOCK, + ID_LITE_BLOCK, + RC_LITE_BLOCK, + CARD_KEY_LITE_BLOCK, + MAC_LITE_BLOCK, + CARD_KEY_VER_LITE_BLOCK, + MEM_CONFIG_LITE_BLOCK, + }; + + uint8_t block_data[FELICA_BLOCK_SIZE * 4]; + + tx_rx->tx_bits = + 8 * felica_lite_prepare_unencrypted_read(tx_rx->tx_data, reader, true, fixed_blocks, 1); + if(!furi_hal_nfc_tx_rx_full(tx_rx)) { + FURI_LOG_W(TAG, "Bad exchange verifying Lite system code"); + return false; + } + if(felica_parse_unencrypted_read( + tx_rx->rx_data, tx_rx->rx_bits / 8, reader, block_data, sizeof(block_data)) != + FELICA_BLOCK_SIZE) { + FURI_LOG_W(TAG, "Bad response to Read without Encryption (SYS_C)"); + return false; + } + if(nfc_util_bytes2num(block_data, 2) != LITE_SYSTEM_CODE) { + FURI_LOG_W(TAG, "Unexpected SYS_C value"); + return false; + } + + tx_rx->tx_bits = 8 * felica_lite_prepare_unencrypted_read( + tx_rx->tx_data, reader, true, &fixed_blocks[1], 1); + if(!furi_hal_nfc_tx_rx_full(tx_rx)) { + FURI_LOG_W(TAG, "Bad exchange reading D_ID"); + return false; + } + if(felica_parse_unencrypted_read( + tx_rx->rx_data, tx_rx->rx_bits / 8, reader, block_data, sizeof(block_data)) != + FELICA_BLOCK_SIZE) { + FURI_LOG_W(TAG, "Bad response to Read without Encryption (D_ID)"); + return false; + } + if(memcmp(system->idm, block_data, 8) != 0 || memcmp(system->pmm, block_data + 8, 8) != 0) { + FURI_LOG_W(TAG, "Mismatching values for D_ID"); + return false; + } + + system->code = LITE_SYSTEM_CODE; + + FelicaLiteInfo* lite_info = &system->lite_info; + lite_info->card_key_1 = NULL; + lite_info->card_key_2 = NULL; + + tx_rx->tx_bits = 8 * felica_lite_prepare_unencrypted_read( + tx_rx->tx_data, reader, true, &fixed_blocks[2], 1); + if(!furi_hal_nfc_tx_rx_full(tx_rx)) { + FURI_LOG_W(TAG, "Bad exchange reading ID"); + return false; + } + if(felica_parse_unencrypted_read( + tx_rx->rx_data, tx_rx->rx_bits / 8, reader, block_data, sizeof(block_data)) != + FELICA_BLOCK_SIZE) { + FURI_LOG_W(TAG, "Bad response to Read without Encryption (ID)"); + return false; + } + lite_info->data_format_code = nfc_util_bytes2num(block_data + 8, 2); + memcpy(lite_info->ID_value, block_data + 10, 6); + FURI_LOG_I(TAG, "ID:"); + for(int i = 0; i < FELICA_BLOCK_SIZE; i++) { + FURI_LOG_I(TAG, "%02X", block_data[i]); + } + + memset(block_data, 0, FELICA_BLOCK_SIZE); + tx_rx->tx_bits = 8 * felica_lite_prepare_unencrypted_write( + tx_rx->tx_data, reader, &fixed_blocks[3], 1, block_data); + if(!furi_hal_nfc_tx_rx_full(tx_rx)) { + FURI_LOG_W(TAG, "Bad exchange writing random challenge"); + return false; + } + if(!felica_parse_unencrypted_write(tx_rx->rx_data, tx_rx->rx_bits / 8, reader)) { + FURI_LOG_W(TAG, "Bad response to Write without Encryption (RC)"); + return false; + } + + tx_rx->tx_bits = 8 * felica_lite_prepare_unencrypted_read( + tx_rx->tx_data, reader, true, &fixed_blocks[4], 2); + if(!furi_hal_nfc_tx_rx_full(tx_rx)) { + FURI_LOG_W(TAG, "Bad exchange reading CK and MAC"); + return false; + } + if(felica_parse_unencrypted_read( + tx_rx->rx_data, tx_rx->rx_bits / 8, reader, block_data, sizeof(block_data)) != + FELICA_BLOCK_SIZE * 2) { + FURI_LOG_W(TAG, "Bad response to Read without Encryption (CK, MAC)"); + return false; + } + memcpy(lite_info->MAC, block_data + FELICA_BLOCK_SIZE, 8); + FURI_LOG_I(TAG, "MAC:"); + for(int i = 0; i < FELICA_BLOCK_SIZE; i++) { + FURI_LOG_I(TAG, "%02X", block_data[i + FELICA_BLOCK_SIZE]); + } + + tx_rx->tx_bits = 8 * felica_lite_prepare_unencrypted_read( + tx_rx->tx_data, reader, true, &fixed_blocks[6], 2); + if(!furi_hal_nfc_tx_rx_full(tx_rx)) { + FURI_LOG_W(TAG, "Bad exchange reading CKV and MC"); + return false; + } + if(felica_parse_unencrypted_read( + tx_rx->rx_data, tx_rx->rx_bits / 8, reader, block_data, sizeof(block_data)) != + FELICA_BLOCK_SIZE * 2) { + FURI_LOG_W(TAG, "Bad response to Read without Encryption (CKV, MC)"); + return false; + } + lite_info->card_key_version = nfc_util_bytes2num(block_data, 2); + memcpy(lite_info->memory_config, block_data + FELICA_BLOCK_SIZE, FELICA_BLOCK_SIZE); + + // Read SPAD and REG accordingly to MC + uint8_t* mc_data = lite_info->memory_config; + for(uint8_t block_number = 0; block_number <= REG_LITE_BLOCK; block_number++) { + if(!felica_lite_can_read_without_mac(mc_data + 6, block_number)) { + if(block_number < REG_LITE_BLOCK) { + lite_info->S_PAD[block_number] = NULL; + } else { + lite_info->REG = NULL; + } + continue; + } + + tx_rx->tx_bits = 8 * felica_lite_prepare_unencrypted_read( + tx_rx->tx_data, reader, true, &block_number, 1); + if(!furi_hal_nfc_tx_rx_full(tx_rx)) { + FURI_LOG_W(TAG, "Bad exchange reading blocks"); + return false; + } + if(felica_parse_unencrypted_read( + tx_rx->rx_data, tx_rx->rx_bits / 8, reader, block_data, sizeof(block_data)) != + FELICA_BLOCK_SIZE) { + FURI_LOG_W(TAG, "Bad response to Read without Encryption (block %d)", block_number); + return false; + } + uint8_t* block = malloc(FELICA_BLOCK_SIZE); + memcpy(block, block_data, FELICA_BLOCK_SIZE); + if(block_number < REG_LITE_BLOCK) { + lite_info->S_PAD[block_number] = block; + } else { + lite_info->REG = block; + } + } + if(data->type == FelicaICTypeLiteS) { + const uint8_t fixed_s_blocks[] = { + CARD_KEY_LITE_BLOCK, + MAC_A_LITE_BLOCK, + WRITE_COUNT_LITE_BLOCK, + CRC_CHECK_LITE_BLOCK, + }; + + tx_rx->tx_bits = 8 * felica_lite_prepare_unencrypted_read( + tx_rx->tx_data, reader, true, fixed_s_blocks, 2); + if(!furi_hal_nfc_tx_rx_full(tx_rx)) { + FURI_LOG_W(TAG, "Bad exchange reading ID with MAC_A"); + return false; + } + if(felica_parse_unencrypted_read( + tx_rx->rx_data, tx_rx->rx_bits / 8, reader, block_data, sizeof(block_data)) != + FELICA_BLOCK_SIZE * 2) { + FURI_LOG_W(TAG, "Bad response to Read without Encryption (CK, MAC_A)"); + return false; + } + memcpy(lite_info->MAC_A, block_data + FELICA_BLOCK_SIZE, FELICA_BLOCK_SIZE); + + tx_rx->tx_bits = 8 * felica_lite_prepare_unencrypted_read( + tx_rx->tx_data, reader, true, &fixed_s_blocks[2], 2); + if(!furi_hal_nfc_tx_rx_full(tx_rx)) { + FURI_LOG_W(TAG, "Bad exchange reading ID with MAC_A"); + return false; + } + if(felica_parse_unencrypted_read( + tx_rx->rx_data, tx_rx->rx_bits / 8, reader, block_data, sizeof(block_data)) != + FELICA_BLOCK_SIZE * 2) { + FURI_LOG_W(TAG, "Bad response to Read without Encryption (WC, CRC_CHECK)"); + return false; + } + lite_info->write_count = nfc_util_bytes2num(block_data, 3); + lite_info->crc_valid = block_data[FELICA_BLOCK_SIZE] == 0x00; + } + + return true; +} + +bool felica_read_card( + FuriHalNfcTxRxContext* tx_rx, + FelicaData* data, + uint8_t* polled_idm, + uint8_t* polled_pmm) { + furi_assert(tx_rx); + furi_assert(polled_idm); + furi_assert(polled_pmm); + + bool card_read = false; + do { + FelicaReader reader; + memcpy(reader.current_idm, polled_idm, 8); + memcpy(reader.current_pmm, polled_pmm, 8); + + FelicaSystem* current_system = malloc(sizeof(FelicaSystem)); + FelicaSystemList_init(data->systems); + FelicaSystemList_push_back(data->systems, current_system); + + felica_parse_system_info(current_system, polled_idm, polled_pmm); + + if(data->type == FelicaICTypeLite || data->type == FelicaICTypeLiteS) { + FURI_LOG_I(TAG, "Reading Felica Lite system"); + felica_read_lite_system(tx_rx, &reader, data, current_system); + card_read = true; + break; + } + } while(false); + + return card_read; +} + +void felica_service_clear(FelicaService* service) { + FelicaBlockList_it_t it; + for(FelicaBlockList_it(it, service->blocks); !FelicaBlockList_end_p(it); + FelicaBlockList_next(it)) { + FelicaBlock* block = *FelicaBlockList_ref(it); + free(block); + } + FelicaBlockList_clear(service->blocks); +} + +void felica_lite_clear(FelicaLiteInfo* lite_info) { + for(int i = 0; i < REG_LITE_BLOCK; i++) { + uint8_t* block = lite_info->S_PAD[i]; + if(block != NULL) { + free(block); + } + } + + if(lite_info->REG != NULL) { + free(lite_info->REG); + } + + if(lite_info->card_key_1 != NULL) { + free(lite_info->card_key_1); + } + if(lite_info->card_key_2 != NULL) { + free(lite_info->card_key_2); + } +} + +void felica_area_clear(FelicaArea* area) { + FelicaNodeList_it_t it; + for(FelicaNodeList_it(it, area->nodes); !FelicaNodeList_end_p(it); FelicaNodeList_next(it)) { + FelicaNode* node = *FelicaNodeList_ref(it); + if(node->type == FelicaNodeTypeArea) { + felica_area_clear(node->area); + } else if(node->type == FelicaNodeTypeService) { + felica_service_clear(node->service); + } + free(node); + } + FelicaNodeList_clear(area->nodes); +} + +void felica_clear(FelicaData* data) { + FelicaSystemList_it_t it; + for(FelicaSystemList_it(it, data->systems); !FelicaSystemList_end_p(it); + FelicaSystemList_next(it)) { + FelicaSystem* system = *FelicaSystemList_ref(it); + if(system->code == LITE_SYSTEM_CODE) { + felica_lite_clear(&system->lite_info); + ; + } else { + felica_area_clear(&system->root_area); + } + } + FelicaSystemList_clear(data->systems); +} \ No newline at end of file diff --git a/lib/nfc/protocols/felica.h b/lib/nfc/protocols/felica.h new file mode 100644 index 000000000..fa81cba2f --- /dev/null +++ b/lib/nfc/protocols/felica.h @@ -0,0 +1,270 @@ +#pragma once + +#include +#include +#include + +#define NFCF_F_SIG (13560000.0) +#define MRT_T_SIG 302064.89 //ns, 256 * 16 / NFC_F_SIG * 1e9 +#define MRT_T_SIG_x4 1208259.56 //ns, MRT_T_SIG * (4 ** 1) +#define MRT_T_SIG_x16 4833038.24 //ns, MRT_T_SIG * (4 ** 2) +#define MRT_T_SIG_x64 19332152.96 //ns, MRT_T_SIG * (4 ** 2) + +#define FELICA_VARIABLE_MRT 0 +#define FELICA_FIXED_MRT 1 +#define FELICA_MUTUAL_AUTH_MRT 2 +#define FELICA_READ_MRT 3 +#define FELICA_WRITE_MRT 4 +#define FELICA_OTHER_MRT 5 + +#define FELICA_BLOCK_SIZE 16 + +#define CYBERNET_SYSTEM_CODE 0x0003 +#define NDEF_SYSTEM_CODE 0x12fc +#define HCE_F_SYSTEM_CODE 0x4000 +#define OCTOPUS_SYSTEM_CODE 0x8008 +#define IRUCA_SYSTEM_CODE 0x80de +#define EDY_SYSTEM_CODE 0x811d +#define PASPY_SYSTEM_CODE 0x8592 +#define BLACKBOARD_SYSTEM_CODE 0x8620 +#define SAPICA_SYSTEM_CODE 0x865e +#define SUICA_SYSTEM_CODE 0x86a7 +#define LITE_SYSTEM_CODE 0x88b4 +#define RYUTO_SYSTEM_CODE 0x8b5d +#define OKICA_SYSTEM_CODE 0x8fc1 +#define SECURE_ID_SYSTEM_CODE 0x957a +#define COMMON_AREA_SYSTEM_CODE 0xfe00 +#define PLUG_SYSTEM_CODE 0xfee1 + +#define REG_LITE_BLOCK 0x0e +#define RC_LITE_BLOCK 0x80 +#define MAC_LITE_BLOCK 0x81 +#define ID_LITE_BLOCK 0x82 +#define DEVICE_ID_LITE_BLOCK 0x83 +#define SERVICE_CODE_LITE_BLOCK 0x84 +#define SYS_CODE_LITE_BLOCK 0x85 +#define CARD_KEY_VER_LITE_BLOCK 0x86 +#define CARD_KEY_LITE_BLOCK 0x87 +#define MEM_CONFIG_LITE_BLOCK 0x88 +#define WRITE_COUNT_LITE_BLOCK 0x90 +#define MAC_A_LITE_BLOCK 0x91 +#define STATE_LITE_BLOCK 0x92 +#define CRC_CHECK_LITE_BLOCK 0xA0 + +#define IS_2_BYTE_BLOCK_LIST_ELEMENT 0x80 + +#define FELICA_UNENCRYPTED_READ_CMD 0x06 +#define FELICA_UNENCRYPTED_WRITE_CMD 0x08 + +#define FELICA_UNENCRYPTED_READ_RES 0x07 +#define FELICA_UNENCRYPTED_WRITE_RES 0x09 + +typedef enum { + FelicaICTypeRC_SA24_10K, // RC-SA24/1x + FelicaICTypeRC_SA24_6K, // RC-SA24/1x1 + FelicaICTypeSD2_6K, // RC-SA21/2x1 + FelicaICTypeSD2_4K, // RC-SA21/2 + FelicaICTypeSD2WithDES, // RC-SA20/1, RC-SA20/2 + FelicaICTypeRC_SA08, // Certifications exist, prototype? + FelicaICTypeSD1, // RC-SA01 + FelicaICTypeSD1WithDES, // RC-SA00 + FelicaICTypeFRAM_4K, // RC-S962 + FelicaICTypeFRAM_9K, // RC-S960 + FelicaICTypeEMV_36K, // RC-S954 + FelicaICTypeEMV_16K, // RC-S953 + FelicaICTypeEMV_32K, // RC-S952 + FelicaICType576B, // RC-S919 + FelicaICType4K, // RC-S915 + FelicaICType2K, // RC-S830 series cards, chip name unknown, + FelicaICTypeMobileIC_V4_1, + FelicaICTypeMobileIC_V4, + FelicaICTypeMobileIC_V3, + FelicaICTypeMobileIC_V2, + FelicaICTypeMobileIC_V1, + FelicaICTypeLite, // RC-S965 + FelicaICTypeLiteS, // RC-S966 + FelicaICTypeLink, // RC-S967, + FelicaICTypePlug, // RC-S926 + FelicaICTypeSuica, // https://www.tuv-nederland.nl/assets/files/cerfiticaten/2019/07/cr-nscib-cc-10-30076-cr.pdf +} FelicaICType; + +typedef struct { + uint8_t exponent : 2; + // Incremented at read + uint8_t real_a : 4; + uint8_t real_b : 4; +} FelicaMRTParts; + +typedef enum { + FelicaServiceTypeRandom = (0b0010 << 2), + FelicaServiceTypeCyclic = (0b0011 << 2), + FelicaServiceTypePurse = (0b010 << 3), +} FelicaServiceType; +typedef enum { + FelicaServiceAttributeAuthRW = 0b00, + FelicaServiceAttributeUnauthRW = 0b01, + FelicaServiceAttributeAuthRO = 0b10, + FelicaServiceAttributeUnauthRO = 0b11, + + FelicaServiceAttributeAuthDirectAccess = 0b000, + FelicaServiceAttributeUnauthDirectAccess = 0b001, + FelicaServiceAttributeAuthCashbackDecrement = 0b010, + FelicaServiceAttributeUnauthCashbackDecrement = 0b011, + FelicaServiceAttributeAuthDecrement = 0b100, + FelicaServiceAttributeUnauthDecrement = 0b101, + FelicaServiceAttributeAuthPurseRO = 0b110, + FelicaServiceAttributeUnauthPurseRO = 0b111, +} FelicaServiceAttribute; + +DICT_SET_DEF( + FelicaServiceAttributeList, + FelicaServiceAttribute, + M_ENUM_OPLIST(FelicaServiceAttribute, FelicaServiceAttributeAuthRW)) + +typedef FelicaMRTParts FelicaMRTParameters[6]; + +typedef struct { + uint8_t data[FELICA_BLOCK_SIZE]; +} FelicaBlock; + +ARRAY_DEF(FelicaBlockList, FelicaBlock*, M_PTR_OPLIST) + +typedef struct { + uint16_t number; + FelicaServiceAttributeList_t access_control_list; // accounts for overlap services + bool is_extended_overlap; + union { + FelicaBlockList_t blocks; + struct { + uint16_t overlap_target; + uint8_t block_start; + uint8_t block_count; + }; + }; +} FelicaService; + +typedef enum { + FelicaNodeTypeArea, + FelicaNodeTypeService, +} FelicaNodeType; + +struct _FelicaArea_t; +typedef struct { + FelicaNodeType type; + union { + struct _FelicaArea_t* area; + FelicaService* service; + }; +} FelicaNode; + +ARRAY_DEF(FelicaNodeList, FelicaNode*, M_PTR_OPLIST) + +typedef struct _FelicaArea_t { + uint16_t number; + bool can_create_subareas; + uint16_t end_service_code; + + FelicaNodeList_t nodes; +} FelicaArea; + +typedef struct { + uint8_t* S_PAD[14]; + uint8_t* REG; + // MACs of all zero bytes (read from CK) + uint8_t MAC[8]; + uint16_t data_format_code; + uint8_t ID_value[6]; + uint8_t* card_key_1; + uint8_t* card_key_2; + uint16_t card_key_version; + uint8_t memory_config[FELICA_BLOCK_SIZE]; + + // Lite-S only + uint8_t MAC_A[8]; + uint32_t write_count; + bool crc_valid; +} FelicaLiteInfo; + +typedef struct _FelicaSystem_t { + uint8_t number; + uint16_t code; + uint8_t idm[8]; + uint8_t pmm[8]; + FelicaMRTParameters maximum_response_times; + + union { + FelicaLiteInfo lite_info; + FelicaArea root_area; + }; +} FelicaSystem; + +ARRAY_DEF(FelicaSystemList, FelicaSystem*, M_PTR_OPLIST) + +typedef struct { + FelicaICType type; + uint8_t subtype; + FelicaSystemList_t systems; +} FelicaData; + +typedef struct { + uint8_t current_idm[8]; + uint8_t current_pmm[8]; + + uint8_t status_flags[2]; +} FelicaReader; + +bool felica_check_ic_type(uint8_t* PMm); +FelicaICType felica_get_ic_type(uint8_t* PMm); + +uint8_t felica_prepare_unencrypted_read( + uint8_t* dest, + const FelicaReader* reader, + const uint16_t* service_code_list, + uint8_t service_count, + const uint32_t* block_list, + uint8_t block_count); +uint8_t felica_lite_prepare_unencrypted_read( + uint8_t* dest, + const FelicaReader* reader, + bool is_read_only, + const uint8_t* block_list, + uint8_t block_count); +uint16_t felica_parse_unencrypted_read( + uint8_t* buf, + uint8_t len, + FelicaReader* reader, + uint8_t* out, + uint16_t out_len); + +uint8_t felica_prepare_unencrypted_write( + uint8_t* dest, + FelicaReader* reader, + const uint16_t* service_code_list, + uint8_t service_count, + const uint32_t* block_list, + uint8_t block_count, + const uint8_t* block_data); +uint8_t felica_lite_prepare_unencrypted_write( + uint8_t* dest, + const FelicaReader* reader, + const uint8_t* block_list, + uint8_t block_count, + const uint8_t* block_data); +bool felica_parse_unencrypted_write(uint8_t* buf, uint8_t len, FelicaReader* reader); + +bool felica_lite_can_read_without_mac(uint8_t* mc_r_restr, uint8_t block_number); + +void felica_define_normal_block(FelicaService* service, uint16_t number, uint8_t* data); + +bool felica_read_lite_system( + FuriHalNfcTxRxContext* tx_rx, + FelicaReader* reader, + FelicaData* data, + FelicaSystem* system); + +bool felica_read_card( + FuriHalNfcTxRxContext* tx_rx, + FelicaData* data, + uint8_t* polled_idm, + uint8_t* polled_pmm); +void felica_clear(FelicaData* data); \ No newline at end of file diff --git a/lib/nfc/protocols/felica_util.c b/lib/nfc/protocols/felica_util.c new file mode 100644 index 000000000..a5198aa09 --- /dev/null +++ b/lib/nfc/protocols/felica_util.c @@ -0,0 +1,43 @@ +#include "./felica.h" +#include + +FuriString* felica_get_system_name(FelicaSystem* system) { + uint16_t code = system->code; + + const char* prefix; + if(code == SUICA_SYSTEM_CODE) { + prefix = "SuiCa"; + } else if(code == NDEF_SYSTEM_CODE) { + prefix = "NDEF"; + } else if(code == HCE_F_SYSTEM_CODE) { + prefix = "HCE-F"; + } else if(code == OCTOPUS_SYSTEM_CODE) { + prefix = "Octopus"; + } else if(code == EDY_SYSTEM_CODE) { + prefix = "Edy"; + } else if(code == PASPY_SYSTEM_CODE) { + prefix = "PASPY"; + } else if(code == BLACKBOARD_SYSTEM_CODE) { + prefix = "Blackboard"; + } else if(code == SAPICA_SYSTEM_CODE) { + prefix = "SAPICA"; + } else if(code == LITE_SYSTEM_CODE) { + prefix = "FeliCa Lite"; + } else if(code == RYUTO_SYSTEM_CODE) { + prefix = "Ryuto"; + } else if(code == OKICA_SYSTEM_CODE) { + prefix = "OKICA"; + } else if(code == SECURE_ID_SYSTEM_CODE) { + prefix = "FeliCa Secure ID"; + } else if(code == IRUCA_SYSTEM_CODE) { + prefix = "IruCa"; + } else if(code == COMMON_AREA_SYSTEM_CODE) { + prefix = "Common Area"; + } else if(code == PLUG_SYSTEM_CODE) { + prefix = "FeliCa Plug"; + } else { + return furi_string_alloc_printf("System %04X", code); + } + + return furi_string_alloc_printf("%s (%04X)", prefix, code); +} \ No newline at end of file diff --git a/lib/nfc/protocols/felica_util.h b/lib/nfc/protocols/felica_util.h new file mode 100644 index 000000000..4224668eb --- /dev/null +++ b/lib/nfc/protocols/felica_util.h @@ -0,0 +1,4 @@ +#include "./felica.h" + +FuriString* felica_get_system_name(FelicaSystem* system); +FuriString* felica_get_service_name(FelicaService* service); \ No newline at end of file diff --git a/lib/nfc/protocols/mifare_classic.c b/lib/nfc/protocols/mifare_classic.c index d2d7467dc..31c347021 100644 --- a/lib/nfc/protocols/mifare_classic.c +++ b/lib/nfc/protocols/mifare_classic.c @@ -374,8 +374,10 @@ bool mf_classic_is_value_block(MfClassicData* data, uint8_t block_num) { data, block_num, MfClassicKeyB, MfClassicActionDataDec)); } -bool mf_classic_check_card_type(uint8_t ATQA0, uint8_t ATQA1, uint8_t SAK) { - UNUSED(ATQA1); +bool mf_classic_check_card_type(FuriHalNfcADevData* data) { + uint8_t ATQA0 = data->atqa[0]; + uint8_t ATQA1 = data->atqa[1]; + uint8_t SAK = data->sak; if((ATQA0 == 0x44 || ATQA0 == 0x04) && (SAK == 0x08 || SAK == 0x88 || SAK == 0x09)) { return true; } else if((ATQA0 == 0x01) && (ATQA1 == 0x0F) && (SAK == 0x01)) { @@ -388,8 +390,10 @@ bool mf_classic_check_card_type(uint8_t ATQA0, uint8_t ATQA1, uint8_t SAK) { } } -MfClassicType mf_classic_get_classic_type(uint8_t ATQA0, uint8_t ATQA1, uint8_t SAK) { - UNUSED(ATQA1); +MfClassicType mf_classic_get_classic_type(FuriHalNfcADevData* data) { + uint8_t ATQA0 = data->atqa[0]; + uint8_t ATQA1 = data->atqa[1]; + uint8_t SAK = data->sak; if((ATQA0 == 0x44 || ATQA0 == 0x04)) { if((SAK == 0x08 || SAK == 0x88)) { return MfClassicType1k; diff --git a/lib/nfc/protocols/mifare_classic.h b/lib/nfc/protocols/mifare_classic.h index c03350f2e..f39c2ca68 100644 --- a/lib/nfc/protocols/mifare_classic.h +++ b/lib/nfc/protocols/mifare_classic.h @@ -94,9 +94,9 @@ typedef struct { const char* mf_classic_get_type_str(MfClassicType type); -bool mf_classic_check_card_type(uint8_t ATQA0, uint8_t ATQA1, uint8_t SAK); +bool mf_classic_check_card_type(FuriHalNfcADevData* data); -MfClassicType mf_classic_get_classic_type(uint8_t ATQA0, uint8_t ATQA1, uint8_t SAK); +MfClassicType mf_classic_get_classic_type(FuriHalNfcADevData* data); uint8_t mf_classic_get_total_sectors_num(MfClassicType type); diff --git a/lib/nfc/protocols/mifare_common.c b/lib/nfc/protocols/mifare_common.c index 90b57e1f0..cfb5e0a37 100644 --- a/lib/nfc/protocols/mifare_common.c +++ b/lib/nfc/protocols/mifare_common.c @@ -1,6 +1,10 @@ #include "mifare_common.h" +#include "furi_hal_nfc.h" -MifareType mifare_common_get_type(uint8_t ATQA0, uint8_t ATQA1, uint8_t SAK) { +MifareType mifare_common_get_type(FuriHalNfcADevData* data) { + uint8_t ATQA0 = data->atqa[0]; + uint8_t ATQA1 = data->atqa[1]; + uint8_t SAK = data->sak; MifareType type = MifareTypeUnknown; if((ATQA0 == 0x44) && (ATQA1 == 0x00) && (SAK == 0x00)) { diff --git a/lib/nfc/protocols/mifare_common.h b/lib/nfc/protocols/mifare_common.h index 2b694d906..e60a97bd0 100644 --- a/lib/nfc/protocols/mifare_common.h +++ b/lib/nfc/protocols/mifare_common.h @@ -1,6 +1,7 @@ #pragma once #include +#include "furi_hal_nfc.h" typedef enum { MifareTypeUnknown, @@ -9,4 +10,4 @@ typedef enum { MifareTypeDesfire, } MifareType; -MifareType mifare_common_get_type(uint8_t ATQA0, uint8_t ATQA1, uint8_t SAK); +MifareType mifare_common_get_type(FuriHalNfcADevData* data); diff --git a/lib/nfc/protocols/mifare_desfire.c b/lib/nfc/protocols/mifare_desfire.c index 23308ae95..23fbcd703 100644 --- a/lib/nfc/protocols/mifare_desfire.c +++ b/lib/nfc/protocols/mifare_desfire.c @@ -234,7 +234,11 @@ void mf_df_cat_file(MifareDesfireFile* file, FuriString* out) { } } -bool mf_df_check_card_type(uint8_t ATQA0, uint8_t ATQA1, uint8_t SAK) { +bool mf_df_check_card_type(FuriHalNfcADevData* data) { + uint8_t ATQA0 = data->atqa[0]; + uint8_t ATQA1 = data->atqa[1]; + uint8_t SAK = data->sak; + return ATQA0 == 0x44 && ATQA1 == 0x03 && SAK == 0x20; } diff --git a/lib/nfc/protocols/mifare_desfire.h b/lib/nfc/protocols/mifare_desfire.h index 963a18f58..5a912377d 100644 --- a/lib/nfc/protocols/mifare_desfire.h +++ b/lib/nfc/protocols/mifare_desfire.h @@ -128,7 +128,7 @@ void mf_df_cat_application_info(MifareDesfireApplication* app, FuriString* out); void mf_df_cat_application(MifareDesfireApplication* app, FuriString* out); void mf_df_cat_file(MifareDesfireFile* file, FuriString* out); -bool mf_df_check_card_type(uint8_t ATQA0, uint8_t ATQA1, uint8_t SAK); +bool mf_df_check_card_type(FuriHalNfcADevData* data); uint16_t mf_df_prepare_get_version(uint8_t* dest); bool mf_df_parse_get_version_response(uint8_t* buf, uint16_t len, MifareDesfireVersion* out); diff --git a/lib/nfc/protocols/mifare_ultralight.c b/lib/nfc/protocols/mifare_ultralight.c index 0e28c0074..1cea2c392 100644 --- a/lib/nfc/protocols/mifare_ultralight.c +++ b/lib/nfc/protocols/mifare_ultralight.c @@ -33,11 +33,12 @@ uint32_t mf_ul_pwdgen_amiibo(FuriHalNfcDevData* data) { return pwd; } -bool mf_ul_check_card_type(uint8_t ATQA0, uint8_t ATQA1, uint8_t SAK) { - if((ATQA0 == 0x44) && (ATQA1 == 0x00) && (SAK == 0x00)) { - return true; - } - return false; +bool mf_ul_check_card_type(FuriHalNfcADevData* data) { + uint8_t ATQA0 = data->atqa[0]; + uint8_t ATQA1 = data->atqa[1]; + uint8_t SAK = data->sak; + + return ((ATQA0 == 0x44) && (ATQA1 == 0x00) && (SAK == 0x00)); } void mf_ul_reset(MfUltralightData* data) { diff --git a/lib/nfc/protocols/mifare_ultralight.h b/lib/nfc/protocols/mifare_ultralight.h index 4ab22e89c..9ae9ab463 100644 --- a/lib/nfc/protocols/mifare_ultralight.h +++ b/lib/nfc/protocols/mifare_ultralight.h @@ -200,7 +200,7 @@ typedef struct { void mf_ul_reset(MfUltralightData* data); -bool mf_ul_check_card_type(uint8_t ATQA0, uint8_t ATQA1, uint8_t SAK); +bool mf_ul_check_card_type(FuriHalNfcADevData* data); bool mf_ultralight_read_version( FuriHalNfcTxRxContext* tx_rx,