esubghz_chat_key_menu.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. #include "../esubghz_chat_i.h"
  2. typedef enum {
  3. ESubGhzChatKeyMenuItems_NoEncryption,
  4. ESubGhzChatKeyMenuItems_Password,
  5. ESubGhzChatKeyMenuItems_HexKey,
  6. ESubGhzChatKeyMenuItems_GenKey,
  7. } ESubGhzChatKeyMenuItems;
  8. static void key_menu_cb(void* context, uint32_t index) {
  9. furi_assert(context);
  10. ESubGhzChatState* state = context;
  11. uint8_t key[KEY_BITS / 8];
  12. switch(index) {
  13. case ESubGhzChatKeyMenuItems_NoEncryption:
  14. state->encrypted = false;
  15. view_dispatcher_send_custom_event(
  16. state->view_dispatcher, ESubGhzChatEvent_KeyMenuNoEncryption);
  17. break;
  18. case ESubGhzChatKeyMenuItems_Password:
  19. view_dispatcher_send_custom_event(
  20. state->view_dispatcher, ESubGhzChatEvent_KeyMenuPassword);
  21. break;
  22. case ESubGhzChatKeyMenuItems_HexKey:
  23. view_dispatcher_send_custom_event(state->view_dispatcher, ESubGhzChatEvent_KeyMenuHexKey);
  24. break;
  25. case ESubGhzChatKeyMenuItems_GenKey:
  26. /* generate a random key */
  27. furi_hal_random_fill_buf(key, KEY_BITS / 8);
  28. /* initiate the crypto context */
  29. bool ret = crypto_ctx_set_key(state->crypto_ctx, key, state->name_prefix, furi_get_tick());
  30. /* cleanup */
  31. crypto_explicit_bzero(key, sizeof(key));
  32. if(!ret) {
  33. crypto_ctx_clear(state->crypto_ctx);
  34. return;
  35. }
  36. /* set encrypted flag and enter the chat */
  37. state->encrypted = true;
  38. view_dispatcher_send_custom_event(state->view_dispatcher, ESubGhzChatEvent_KeyMenuGenKey);
  39. break;
  40. default:
  41. break;
  42. }
  43. }
  44. /* Prepares the key menu scene. */
  45. void scene_on_enter_key_menu(void* context) {
  46. FURI_LOG_T(APPLICATION_NAME, "scene_on_enter_key_menu");
  47. furi_assert(context);
  48. ESubGhzChatState* state = context;
  49. menu_reset(state->menu);
  50. /* clear the crypto CTX in case we got back from password or hex key
  51. * input */
  52. crypto_ctx_clear(state->crypto_ctx);
  53. menu_add_item(
  54. state->menu,
  55. "No encryption",
  56. &I_chat_14px,
  57. ESubGhzChatKeyMenuItems_NoEncryption,
  58. key_menu_cb,
  59. state);
  60. menu_add_item(
  61. state->menu,
  62. "Password",
  63. &I_keyboard_14px,
  64. ESubGhzChatKeyMenuItems_Password,
  65. key_menu_cb,
  66. state);
  67. menu_add_item(
  68. state->menu, "Hex Key", &I_hex_14px, ESubGhzChatKeyMenuItems_HexKey, key_menu_cb, state);
  69. menu_add_item(
  70. state->menu,
  71. "Generate Key",
  72. &I_u2f_14px,
  73. ESubGhzChatKeyMenuItems_GenKey,
  74. key_menu_cb,
  75. state);
  76. view_dispatcher_switch_to_view(state->view_dispatcher, ESubGhzChatView_Menu);
  77. }
  78. /* Handles scene manager events for the key menu scene. */
  79. bool scene_on_event_key_menu(void* context, SceneManagerEvent event) {
  80. FURI_LOG_T(APPLICATION_NAME, "scene_on_event_key_menu");
  81. furi_assert(context);
  82. ESubGhzChatState* state = context;
  83. bool consumed = false;
  84. switch(event.type) {
  85. case SceneManagerEventTypeCustom:
  86. switch(event.event) {
  87. /* switch to frequency input scene */
  88. case ESubGhzChatEvent_KeyMenuNoEncryption:
  89. case ESubGhzChatEvent_KeyMenuGenKey:
  90. scene_manager_next_scene(state->scene_manager, ESubGhzChatScene_FreqInput);
  91. consumed = true;
  92. break;
  93. /* switch to password input scene */
  94. case ESubGhzChatEvent_KeyMenuPassword:
  95. scene_manager_next_scene(state->scene_manager, ESubGhzChatScene_PassInput);
  96. consumed = true;
  97. break;
  98. /* switch to hex key input scene */
  99. case ESubGhzChatEvent_KeyMenuHexKey:
  100. scene_manager_next_scene(state->scene_manager, ESubGhzChatScene_HexKeyInput);
  101. consumed = true;
  102. break;
  103. }
  104. break;
  105. case SceneManagerEventTypeBack:
  106. /* stop the application if the user presses back here */
  107. view_dispatcher_stop(state->view_dispatcher);
  108. consumed = true;
  109. break;
  110. default:
  111. consumed = false;
  112. break;
  113. }
  114. return consumed;
  115. }
  116. /* Cleans up the key menu scene. */
  117. void scene_on_exit_key_menu(void* context) {
  118. FURI_LOG_T(APPLICATION_NAME, "scene_on_exit_key_menu");
  119. furi_assert(context);
  120. ESubGhzChatState* state = context;
  121. menu_reset(state->menu);
  122. }