esubghz_chat_key_share_popup.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. #include "../esubghz_chat_i.h"
  2. #include "../helpers/nfc_helpers.h"
  3. #include <machine/endian.h>
  4. struct ReplayDictNfcWriterContext {
  5. uint8_t* cur;
  6. uint8_t* max;
  7. };
  8. static bool replay_dict_nfc_writer(uint64_t run_id, uint32_t counter, void* context) {
  9. struct ReplayDictNfcWriterContext* ctx = (struct ReplayDictNfcWriterContext*)context;
  10. struct ReplayDictNfcEntry entry = {.run_id = run_id, .counter = __htonl(counter), .unused = 0};
  11. if(ctx->cur + sizeof(entry) > ctx->max) {
  12. return false;
  13. }
  14. memcpy(ctx->cur, &entry, sizeof(entry));
  15. ctx->cur += sizeof(entry);
  16. return true;
  17. }
  18. static void prepare_nfc_dev_data(ESubGhzChatState* state) {
  19. NfcDeviceData* dev_data = state->nfc_dev_data;
  20. dev_data->protocol = NfcDeviceProtocolMifareUl;
  21. furi_hal_random_fill_buf(dev_data->nfc_data.uid, 7);
  22. dev_data->nfc_data.uid_len = 7;
  23. dev_data->nfc_data.atqa[0] = 0x44;
  24. dev_data->nfc_data.atqa[1] = 0x00;
  25. dev_data->nfc_data.sak = 0x00;
  26. dev_data->mf_ul_data.type = MfUltralightTypeNTAG215;
  27. dev_data->mf_ul_data.version.header = 0x00;
  28. dev_data->mf_ul_data.version.vendor_id = 0x04;
  29. dev_data->mf_ul_data.version.prod_type = 0x04;
  30. dev_data->mf_ul_data.version.prod_subtype = 0x02;
  31. dev_data->mf_ul_data.version.prod_ver_major = 0x01;
  32. dev_data->mf_ul_data.version.prod_ver_minor = 0x00;
  33. dev_data->mf_ul_data.version.storage_size = 0x11;
  34. dev_data->mf_ul_data.version.protocol_type = 0x03;
  35. size_t data_written = 0;
  36. /* write key */
  37. crypto_ctx_get_key(state->crypto_ctx, dev_data->mf_ul_data.data);
  38. data_written += (KEY_BITS / 8);
  39. /* write frequency */
  40. struct FreqNfcEntry* freq_entry =
  41. (struct FreqNfcEntry*)(dev_data->mf_ul_data.data + data_written);
  42. freq_entry->frequency = __htonl(state->frequency);
  43. freq_entry->unused1 = 0;
  44. freq_entry->unused2 = 0;
  45. freq_entry->unused3 = 0;
  46. data_written += sizeof(struct FreqNfcEntry);
  47. /* write the replay dict */
  48. struct ReplayDictNfcWriterContext wr_ctx = {
  49. .cur = dev_data->mf_ul_data.data + data_written,
  50. .max = dev_data->mf_ul_data.data + NFC_MAX_BYTES};
  51. size_t n_entries =
  52. crypto_ctx_dump_replay_dict(state->crypto_ctx, replay_dict_nfc_writer, &wr_ctx);
  53. data_written += n_entries * sizeof(struct ReplayDictNfcEntry);
  54. /* calculate size of data, add 16 for config pages */
  55. dev_data->mf_ul_data.data_size = data_written + (NFC_CONFIG_PAGES * 4);
  56. }
  57. /* Prepares the key share popup scene. */
  58. void scene_on_enter_key_share_popup(void* context) {
  59. FURI_LOG_T(APPLICATION_NAME, "scene_on_enter_key_share_popup");
  60. furi_assert(context);
  61. ESubGhzChatState* state = context;
  62. popup_reset(state->nfc_popup);
  63. popup_disable_timeout(state->nfc_popup);
  64. popup_set_header(state->nfc_popup, "Sharing...", 67, 13, AlignLeft, AlignTop);
  65. popup_set_icon(state->nfc_popup, 0, 3, &I_NFC_dolphin_emulation_51x64);
  66. popup_set_text(state->nfc_popup, "Sharing\nKey via\nNFC", 90, 28, AlignCenter, AlignTop);
  67. prepare_nfc_dev_data(state);
  68. nfc_worker_start(
  69. state->nfc_worker, NfcWorkerStateMfUltralightEmulate, state->nfc_dev_data, NULL, NULL);
  70. notification_message(state->notification, &sequence_blink_start_magenta);
  71. view_dispatcher_switch_to_view(state->view_dispatcher, ESubGhzChatView_NfcPopup);
  72. }
  73. /* Handles scene manager events for the key share popup scene. */
  74. bool scene_on_event_key_share_popup(void* context, SceneManagerEvent event) {
  75. FURI_LOG_T(APPLICATION_NAME, "scene_on_event_key_share_popup");
  76. furi_assert(context);
  77. ESubGhzChatState* state = context;
  78. UNUSED(state);
  79. UNUSED(event);
  80. return false;
  81. }
  82. static bool temp_read_worker_cb(NfcWorkerEvent event, void* context) {
  83. UNUSED(event);
  84. UNUSED(context);
  85. return true;
  86. }
  87. /* Cleans up the key share popup scene. */
  88. void scene_on_exit_key_share_popup(void* context) {
  89. FURI_LOG_T(APPLICATION_NAME, "scene_on_exit_key_share_popup");
  90. furi_assert(context);
  91. ESubGhzChatState* state = context;
  92. popup_reset(state->nfc_popup);
  93. notification_message(state->notification, &sequence_blink_stop);
  94. nfc_worker_stop(state->nfc_worker);
  95. nfc_worker_start(
  96. state->nfc_worker, NfcWorkerStateRead, state->nfc_dev_data, temp_read_worker_cb, state);
  97. nfc_worker_stop(state->nfc_worker);
  98. crypto_explicit_bzero(state->nfc_dev_data->mf_ul_data.data, KEY_BITS / 8);
  99. memset(state->nfc_dev_data, 0, sizeof(NfcDeviceData));
  100. }