esubghz_chat_pass_input.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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. /* cleanup */
  33. crypto_explicit_bzero(key, sizeof(key));
  34. if (!ret) {
  35. crypto_ctx_clear(state->crypto_ctx);
  36. furi_string_printf(error, "Failed to\nset key!");
  37. return false;
  38. }
  39. state->encrypted = true;
  40. return true;
  41. }
  42. /* Prepares the password input scene. */
  43. void scene_on_enter_pass_input(void* context)
  44. {
  45. FURI_LOG_T(APPLICATION_NAME, "scene_on_enter_pass_input");
  46. furi_assert(context);
  47. ESubGhzChatState* state = context;
  48. state->text_input_store[0] = 0;
  49. text_input_reset(state->text_input);
  50. text_input_set_result_callback(
  51. state->text_input,
  52. pass_input_cb,
  53. state,
  54. state->text_input_store,
  55. sizeof(state->text_input_store),
  56. true);
  57. text_input_set_validator(
  58. state->text_input,
  59. pass_input_validator,
  60. state);
  61. text_input_set_header_text(
  62. state->text_input,
  63. "Password");
  64. view_dispatcher_switch_to_view(state->view_dispatcher, ESubGhzChatView_Input);
  65. }
  66. /* Handles scene manager events for the password input scene. */
  67. bool scene_on_event_pass_input(void* context, SceneManagerEvent event)
  68. {
  69. FURI_LOG_T(APPLICATION_NAME, "scene_on_event_pass_input");
  70. furi_assert(context);
  71. ESubGhzChatState* state = context;
  72. bool consumed = false;
  73. switch(event.type) {
  74. case SceneManagerEventTypeCustom:
  75. switch(event.event) {
  76. /* switch to message input scene */
  77. case ESubGhzChatEvent_PassEntered:
  78. scene_manager_next_scene(state->scene_manager,
  79. ESubGhzChatScene_ChatInput);
  80. consumed = true;
  81. break;
  82. }
  83. break;
  84. default:
  85. consumed = false;
  86. break;
  87. }
  88. return consumed;
  89. }
  90. /* Cleans up the password input scene. */
  91. void scene_on_exit_pass_input(void* context)
  92. {
  93. FURI_LOG_T(APPLICATION_NAME, "scene_on_exit_pass_input");
  94. furi_assert(context);
  95. ESubGhzChatState* state = context;
  96. text_input_reset(state->text_input);
  97. crypto_explicit_bzero(state->text_input_store,
  98. sizeof(state->text_input_store));
  99. }