esubghz_chat_key_share_popup.c 3.5 KB

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