totp_scene_authenticate.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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(canvas, SCREEN_WIDTH_CENTER, SCREEN_HEIGHT_CENTER - 10 + v_shift, AlignCenter, AlignCenter, "Use arrow keys");
  32. canvas_draw_str_aligned(canvas, SCREEN_WIDTH_CENTER, SCREEN_HEIGHT_CENTER + 5 + v_shift, AlignCenter, AlignCenter, "to setup new PIN");
  33. } else {
  34. canvas_draw_str_aligned(canvas, SCREEN_WIDTH_CENTER, SCREEN_HEIGHT_CENTER + v_shift, AlignCenter, AlignCenter, "Use arrow keys to enter PIN");
  35. }
  36. const uint8_t PIN_ASTERISK_RADIUS = 3;
  37. const uint8_t PIN_ASTERISK_STEP = (PIN_ASTERISK_RADIUS << 1) + 2;
  38. if (scene_state->code_length > 0) {
  39. uint8_t left_start_x = (scene_state->code_length - 1) * PIN_ASTERISK_STEP >> 1;
  40. for (uint8_t i = 0; i < scene_state->code_length; i++) {
  41. canvas_draw_disc(
  42. canvas,
  43. SCREEN_WIDTH_CENTER - left_start_x + i * PIN_ASTERISK_STEP,
  44. SCREEN_HEIGHT_CENTER + 10,
  45. PIN_ASTERISK_RADIUS);
  46. }
  47. }
  48. }
  49. bool totp_scene_authenticate_handle_event(PluginEvent* const event, PluginState* plugin_state) {
  50. if(event->type == EventTypeKey) {
  51. if (event->input.type == InputTypeLong && event->input.key == InputKeyBack) {
  52. return false;
  53. } else if(event->input.type == InputTypePress) {
  54. SceneState* scene_state = (SceneState *)plugin_state->current_scene_state;
  55. const uint8_t ARROW_UP_CODE = 2;
  56. const uint8_t ARROW_RIGHT_CODE = 8;
  57. const uint8_t ARROW_DOWN_CODE = 11;
  58. const uint8_t ARROW_LEFT_CODE = 5;
  59. switch(event->input.key) {
  60. case InputKeyUp:
  61. if (scene_state->code_length < MAX_CODE_LENGTH) {
  62. scene_state->code_input[scene_state->code_length] = ARROW_UP_CODE;
  63. scene_state->code_length++;
  64. }
  65. break;
  66. case InputKeyDown:
  67. if (scene_state->code_length < MAX_CODE_LENGTH) {
  68. scene_state->code_input[scene_state->code_length] = ARROW_DOWN_CODE;
  69. scene_state->code_length++;
  70. }
  71. break;
  72. case InputKeyRight:
  73. if (scene_state->code_length < MAX_CODE_LENGTH) {
  74. scene_state->code_input[scene_state->code_length] = ARROW_RIGHT_CODE;
  75. scene_state->code_length++;
  76. }
  77. break;
  78. case InputKeyLeft:
  79. if (scene_state->code_length < MAX_CODE_LENGTH) {
  80. scene_state->code_input[scene_state->code_length] = ARROW_LEFT_CODE;
  81. scene_state->code_length++;
  82. }
  83. break;
  84. case InputKeyOk:
  85. totp_crypto_seed_iv(plugin_state, &scene_state->code_input[0], scene_state->code_length);
  86. if (totp_crypto_verify_key(plugin_state)) {
  87. FURI_LOG_D(LOGGING_TAG, "PIN is valid");
  88. totp_scene_director_activate_scene(plugin_state, TotpSceneGenerateToken, NULL);
  89. } else {
  90. FURI_LOG_D(LOGGING_TAG, "PIN is NOT valid");
  91. memset(&scene_state->code_input[0], 0, MAX_CODE_LENGTH);
  92. memset(&plugin_state->iv[0], 0, TOTP_IV_SIZE);
  93. scene_state->code_length = 0;
  94. DialogMessage* message = dialog_message_alloc();
  95. dialog_message_set_buttons(message, "Try again", NULL, NULL);
  96. dialog_message_set_header(message, "You entered\ninvalid PIN", SCREEN_WIDTH_CENTER - 25, SCREEN_HEIGHT_CENTER - 5, AlignCenter, AlignCenter);
  97. dialog_message_set_icon(message, &I_DolphinCommon_56x48, 72, 17);
  98. dialog_message_show(plugin_state->dialogs, message);
  99. dialog_message_free(message);
  100. }
  101. break;
  102. case InputKeyBack:
  103. if (scene_state->code_length > 0) {
  104. scene_state->code_input[scene_state->code_length - 1] = 0;
  105. scene_state->code_length--;
  106. }
  107. break;
  108. }
  109. }
  110. }
  111. return true;
  112. }
  113. void totp_scene_authenticate_deactivate(PluginState* plugin_state) {
  114. if (plugin_state->current_scene_state == NULL) return;
  115. free(plugin_state->current_scene_state);
  116. plugin_state->current_scene_state = NULL;
  117. }
  118. void totp_scene_authenticate_free(PluginState* plugin_state) {
  119. UNUSED(plugin_state);
  120. }