totp_scene_authenticate.c 5.9 KB

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