totp_scene_authenticate.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. #include "totp_scene_authenticate.h"
  2. #include <dialogs/dialogs.h>
  3. #include <totp_icons.h>
  4. #include "../../types/common.h"
  5. #include "../../services/ui/constants.h"
  6. #include "../../services/config/config.h"
  7. #include "../scene_director.h"
  8. #include "../totp_scenes_enum.h"
  9. #include "../../services/crypto/crypto.h"
  10. #define MAX_CODE_LENGTH TOTP_IV_SIZE
  11. #define ARROW_UP_CODE 2
  12. #define ARROW_RIGHT_CODE 8
  13. #define ARROW_DOWN_CODE 11
  14. #define ARROW_LEFT_CODE 5
  15. typedef struct {
  16. uint8_t code_input[MAX_CODE_LENGTH];
  17. uint8_t code_length;
  18. } SceneState;
  19. void totp_scene_authenticate_init(PluginState* plugin_state) {
  20. memset(&plugin_state->iv[0], 0, TOTP_IV_SIZE);
  21. }
  22. void totp_scene_authenticate_activate(PluginState* plugin_state) {
  23. SceneState* scene_state = malloc(sizeof(SceneState));
  24. furi_check(scene_state != NULL);
  25. scene_state->code_length = 0;
  26. memset(&scene_state->code_input[0], 0, MAX_CODE_LENGTH);
  27. plugin_state->current_scene_state = scene_state;
  28. memset(&plugin_state->iv[0], 0, TOTP_IV_SIZE);
  29. }
  30. void totp_scene_authenticate_render(Canvas* const canvas, PluginState* plugin_state) {
  31. const SceneState* scene_state = (SceneState*)plugin_state->current_scene_state;
  32. int v_shift = 0;
  33. if(scene_state->code_length > 0) {
  34. v_shift = -10;
  35. }
  36. if(plugin_state->crypto_verify_data == NULL) {
  37. canvas_draw_str_aligned(
  38. canvas,
  39. SCREEN_WIDTH_CENTER,
  40. SCREEN_HEIGHT_CENTER - 10 + v_shift,
  41. AlignCenter,
  42. AlignCenter,
  43. "Use arrow keys");
  44. canvas_draw_str_aligned(
  45. canvas,
  46. SCREEN_WIDTH_CENTER,
  47. SCREEN_HEIGHT_CENTER + 5 + v_shift,
  48. AlignCenter,
  49. AlignCenter,
  50. "to setup new PIN");
  51. } else {
  52. canvas_draw_str_aligned(
  53. canvas,
  54. SCREEN_WIDTH_CENTER,
  55. SCREEN_HEIGHT_CENTER + v_shift,
  56. AlignCenter,
  57. AlignCenter,
  58. "Use arrow keys to enter PIN");
  59. }
  60. const uint8_t PIN_ASTERISK_RADIUS = 3;
  61. const uint8_t PIN_ASTERISK_STEP = (PIN_ASTERISK_RADIUS << 1) + 2;
  62. if(scene_state->code_length > 0) {
  63. uint8_t left_start_x = ((scene_state->code_length - 1) * PIN_ASTERISK_STEP) >> 1;
  64. for(uint8_t i = 0; i < scene_state->code_length; i++) {
  65. canvas_draw_disc(
  66. canvas,
  67. SCREEN_WIDTH_CENTER - left_start_x + i * PIN_ASTERISK_STEP,
  68. SCREEN_HEIGHT_CENTER + 10,
  69. PIN_ASTERISK_RADIUS);
  70. }
  71. }
  72. }
  73. bool totp_scene_authenticate_handle_event(
  74. const PluginEvent* const event,
  75. PluginState* plugin_state) {
  76. if(event->type != EventTypeKey) {
  77. return true;
  78. }
  79. if(event->input.type == InputTypeLong && event->input.key == InputKeyBack) {
  80. return false;
  81. }
  82. if(event->input.type != InputTypePress) {
  83. return true;
  84. }
  85. SceneState* scene_state = (SceneState*)plugin_state->current_scene_state;
  86. switch(event->input.key) {
  87. case InputKeyUp:
  88. if(scene_state->code_length < MAX_CODE_LENGTH) {
  89. scene_state->code_input[scene_state->code_length] = ARROW_UP_CODE;
  90. scene_state->code_length++;
  91. }
  92. break;
  93. case InputKeyDown:
  94. if(scene_state->code_length < MAX_CODE_LENGTH) {
  95. scene_state->code_input[scene_state->code_length] = ARROW_DOWN_CODE;
  96. scene_state->code_length++;
  97. }
  98. break;
  99. case InputKeyRight:
  100. if(scene_state->code_length < MAX_CODE_LENGTH) {
  101. scene_state->code_input[scene_state->code_length] = ARROW_RIGHT_CODE;
  102. scene_state->code_length++;
  103. }
  104. break;
  105. case InputKeyLeft:
  106. if(scene_state->code_length < MAX_CODE_LENGTH) {
  107. scene_state->code_input[scene_state->code_length] = ARROW_LEFT_CODE;
  108. scene_state->code_length++;
  109. }
  110. break;
  111. case InputKeyOk:
  112. totp_crypto_seed_iv(plugin_state, &scene_state->code_input[0], scene_state->code_length);
  113. if(totp_crypto_verify_key(plugin_state)) {
  114. FURI_LOG_D(LOGGING_TAG, "PIN is valid");
  115. totp_scene_director_activate_scene(plugin_state, TotpSceneGenerateToken, NULL);
  116. } else {
  117. FURI_LOG_D(LOGGING_TAG, "PIN is NOT valid");
  118. memset(&scene_state->code_input[0], 0, MAX_CODE_LENGTH);
  119. memset(&plugin_state->iv[0], 0, TOTP_IV_SIZE);
  120. scene_state->code_length = 0;
  121. DialogMessage* message = dialog_message_alloc();
  122. dialog_message_set_buttons(message, "Try again", NULL, NULL);
  123. dialog_message_set_header(
  124. message,
  125. "You entered\ninvalid PIN",
  126. SCREEN_WIDTH_CENTER - 25,
  127. SCREEN_HEIGHT_CENTER - 5,
  128. AlignCenter,
  129. AlignCenter);
  130. dialog_message_set_icon(message, &I_DolphinCommon_56x48, 72, 17);
  131. dialog_message_show(plugin_state->dialogs, message);
  132. dialog_message_free(message);
  133. }
  134. break;
  135. case InputKeyBack:
  136. if(scene_state->code_length > 0) {
  137. scene_state->code_input[scene_state->code_length - 1] = 0;
  138. scene_state->code_length--;
  139. }
  140. break;
  141. default:
  142. break;
  143. }
  144. return true;
  145. }
  146. void totp_scene_authenticate_deactivate(PluginState* plugin_state) {
  147. if(plugin_state->current_scene_state == NULL) return;
  148. free(plugin_state->current_scene_state);
  149. plugin_state->current_scene_state = NULL;
  150. }
  151. void totp_scene_authenticate_free(const PluginState* plugin_state) {
  152. UNUSED(plugin_state);
  153. }