esubghz_chat_pass_input.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. #include "../esubghz_chat_i.h"
  2. /* Sends PassEntered event to scene manager and enters the chat. */
  3. static void pass_input_cb(void *context)
  4. {
  5. furi_assert(context);
  6. ESubGhzChatState* state = context;
  7. crypto_explicit_bzero(state->text_input_store,
  8. sizeof(state->text_input_store));
  9. enter_chat(state);
  10. view_dispatcher_send_custom_event(state->view_dispatcher,
  11. ESubGhzChatEvent_PassEntered);
  12. }
  13. /* If a password was entered this derives a key from the password using a
  14. * single pass of SHA256 and initiates the AES-GCM context for encryption. If
  15. * the initiation fails, the password is rejected. */
  16. static bool pass_input_validator(const char *text, FuriString *error,
  17. void *context)
  18. {
  19. furi_assert(text);
  20. furi_assert(error);
  21. furi_assert(context);
  22. ESubGhzChatState* state = context;
  23. if (strlen(text) == 0) {
  24. furi_string_printf(error, "Enter a\npassword!");
  25. return false;
  26. }
  27. unsigned char key[KEY_BITS / 8];
  28. /* derive a key from the password */
  29. sha256((unsigned char *) text, strlen(text), key);
  30. /* initiate the crypto context */
  31. bool ret = crypto_ctx_set_key(state->crypto_ctx, key,
  32. state->name_prefix, furi_get_tick());
  33. /* cleanup */
  34. crypto_explicit_bzero(key, sizeof(key));
  35. if (!ret) {
  36. crypto_ctx_clear(state->crypto_ctx);
  37. furi_string_printf(error, "Failed to\nset key!");
  38. return false;
  39. }
  40. state->encrypted = true;
  41. return true;
  42. }
  43. /* Prepares the password input scene. */
  44. void scene_on_enter_pass_input(void* context)
  45. {
  46. FURI_LOG_T(APPLICATION_NAME, "scene_on_enter_pass_input");
  47. furi_assert(context);
  48. ESubGhzChatState* state = context;
  49. state->text_input_store[0] = 0;
  50. text_input_reset(state->text_input);
  51. text_input_set_result_callback(
  52. state->text_input,
  53. pass_input_cb,
  54. state,
  55. state->text_input_store,
  56. sizeof(state->text_input_store),
  57. true);
  58. text_input_set_validator(
  59. state->text_input,
  60. pass_input_validator,
  61. state);
  62. text_input_set_header_text(
  63. state->text_input,
  64. "Password");
  65. view_dispatcher_switch_to_view(state->view_dispatcher, ESubGhzChatView_Input);
  66. }
  67. /* Handles scene manager events for the password input scene. */
  68. bool scene_on_event_pass_input(void* context, SceneManagerEvent event)
  69. {
  70. FURI_LOG_T(APPLICATION_NAME, "scene_on_event_pass_input");
  71. furi_assert(context);
  72. ESubGhzChatState* state = context;
  73. bool consumed = false;
  74. switch(event.type) {
  75. case SceneManagerEventTypeCustom:
  76. switch(event.event) {
  77. /* switch to message input scene */
  78. case ESubGhzChatEvent_PassEntered:
  79. scene_manager_next_scene(state->scene_manager,
  80. ESubGhzChatScene_ChatInput);
  81. consumed = true;
  82. break;
  83. }
  84. break;
  85. default:
  86. consumed = false;
  87. break;
  88. }
  89. return consumed;
  90. }
  91. /* Cleans up the password input scene. */
  92. void scene_on_exit_pass_input(void* context)
  93. {
  94. FURI_LOG_T(APPLICATION_NAME, "scene_on_exit_pass_input");
  95. furi_assert(context);
  96. ESubGhzChatState* state = context;
  97. text_input_reset(state->text_input);
  98. crypto_explicit_bzero(state->text_input_store,
  99. sizeof(state->text_input_store));
  100. }