generate_totp_code.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. #include "generate_totp_code.h"
  2. #include <furi/core/thread.h>
  3. #include "../../services/crypto/crypto.h"
  4. #include "../../services/totp/totp.h"
  5. #include "../../services/convert/convert.h"
  6. #include <furi_hal_rtc.h>
  7. #include <memset_s.h>
  8. #define ONE_SEC_MS (1000)
  9. struct TotpGenerateCodeWorkerContext {
  10. char* code_buffer;
  11. FuriThread* thread;
  12. FuriMutex* code_buffer_sync;
  13. const TokenInfo* token_info;
  14. float timezone_offset;
  15. uint8_t* iv;
  16. TOTP_NEW_CODE_GENERATED_HANDLER on_new_code_generated_handler;
  17. void* on_new_code_generated_handler_context;
  18. TOTP_CODE_LIFETIME_CHANGED_HANDLER on_code_lifetime_changed_handler;
  19. void* on_code_lifetime_changed_handler_context;
  20. };
  21. static const char* STEAM_ALGO_ALPHABET = "23456789BCDFGHJKMNPQRTVWXY";
  22. static void
  23. int_token_to_str(uint64_t i_token_code, char* str, TokenDigitsCount len, TokenHashAlgo algo) {
  24. char* last_char = str + len;
  25. *last_char = '\0';
  26. if(i_token_code == OTP_ERROR) {
  27. memset(str, '-', len);
  28. } else {
  29. if(algo == STEAM) {
  30. char* s = str;
  31. for(uint8_t i = 0; i < len; i++, s++) {
  32. *s = STEAM_ALGO_ALPHABET[i_token_code % 26];
  33. i_token_code = i_token_code / 26;
  34. }
  35. } else {
  36. char* s = --last_char;
  37. for(int8_t i = len - 1; i >= 0; i--, s--) {
  38. *s = CONVERT_DIGIT_TO_CHAR(i_token_code % 10);
  39. i_token_code = i_token_code / 10;
  40. }
  41. }
  42. }
  43. }
  44. static TOTP_ALGO get_totp_algo_impl(TokenHashAlgo algo) {
  45. switch(algo) {
  46. case SHA1:
  47. case STEAM:
  48. return TOTP_ALGO_SHA1;
  49. case SHA256:
  50. return TOTP_ALGO_SHA256;
  51. case SHA512:
  52. return TOTP_ALGO_SHA512;
  53. default:
  54. break;
  55. }
  56. return NULL;
  57. }
  58. static void generate_totp_code(
  59. TotpGenerateCodeWorkerContext* context,
  60. const TokenInfo* token_info,
  61. uint32_t current_ts) {
  62. if(token_info->token != NULL && token_info->token_length > 0) {
  63. size_t key_length;
  64. uint8_t* key = totp_crypto_decrypt(
  65. token_info->token, token_info->token_length, context->iv, &key_length);
  66. int_token_to_str(
  67. totp_at(
  68. get_totp_algo_impl(token_info->algo),
  69. key,
  70. key_length,
  71. current_ts,
  72. context->timezone_offset,
  73. token_info->duration),
  74. context->code_buffer,
  75. token_info->digits,
  76. token_info->algo);
  77. memset_s(key, key_length, 0, key_length);
  78. free(key);
  79. } else {
  80. int_token_to_str(0, context->code_buffer, token_info->digits, token_info->algo);
  81. }
  82. }
  83. static int32_t totp_generate_worker_callback(void* context) {
  84. furi_check(context);
  85. TotpGenerateCodeWorkerContext* t_context = context;
  86. while(true) {
  87. uint32_t flags = furi_thread_flags_wait(
  88. TotpGenerateCodeWorkerEventStop | TotpGenerateCodeWorkerEventForceUpdate,
  89. FuriFlagWaitAny,
  90. ONE_SEC_MS);
  91. if(flags ==
  92. (uint32_t)
  93. FuriFlagErrorTimeout) { // If timeout, consider as no error, as we expect this and can handle gracefully
  94. flags = 0;
  95. }
  96. furi_check((flags & FuriFlagError) == 0); //-V562
  97. if(flags & TotpGenerateCodeWorkerEventStop) break;
  98. const TokenInfo* token_info = t_context->token_info;
  99. if(token_info == NULL) {
  100. continue;
  101. }
  102. uint32_t curr_ts = furi_hal_rtc_get_timestamp();
  103. bool time_left = false;
  104. if(flags & TotpGenerateCodeWorkerEventForceUpdate ||
  105. (time_left = (curr_ts % token_info->duration) == 0)) {
  106. if(furi_mutex_acquire(t_context->code_buffer_sync, FuriWaitForever) == FuriStatusOk) {
  107. generate_totp_code(t_context, token_info, curr_ts);
  108. curr_ts = furi_hal_rtc_get_timestamp();
  109. furi_mutex_release(t_context->code_buffer_sync);
  110. if(t_context->on_new_code_generated_handler != NULL) {
  111. (*(t_context->on_new_code_generated_handler))(
  112. time_left, t_context->on_new_code_generated_handler_context);
  113. }
  114. }
  115. }
  116. if(t_context->on_code_lifetime_changed_handler != NULL) {
  117. (*(t_context->on_code_lifetime_changed_handler))(
  118. (float)(token_info->duration - curr_ts % token_info->duration) /
  119. (float)token_info->duration,
  120. t_context->on_code_lifetime_changed_handler_context);
  121. }
  122. }
  123. return 0;
  124. }
  125. TotpGenerateCodeWorkerContext* totp_generate_code_worker_start(
  126. char* code_buffer,
  127. const TokenInfo* token_info,
  128. FuriMutex* code_buffer_sync,
  129. float timezone_offset,
  130. uint8_t* iv) {
  131. TotpGenerateCodeWorkerContext* context = malloc(sizeof(TotpGenerateCodeWorkerContext));
  132. furi_check(context != NULL);
  133. context->code_buffer = code_buffer;
  134. context->token_info = token_info;
  135. context->code_buffer_sync = code_buffer_sync;
  136. context->timezone_offset = timezone_offset;
  137. context->iv = iv;
  138. context->thread = furi_thread_alloc();
  139. furi_thread_set_name(context->thread, "TOTPGenerateWorker");
  140. furi_thread_set_stack_size(context->thread, 2048);
  141. furi_thread_set_context(context->thread, context);
  142. furi_thread_set_callback(context->thread, totp_generate_worker_callback);
  143. furi_thread_start(context->thread);
  144. return context;
  145. }
  146. void totp_generate_code_worker_stop(TotpGenerateCodeWorkerContext* context) {
  147. furi_check(context != NULL);
  148. furi_thread_flags_set(furi_thread_get_id(context->thread), TotpGenerateCodeWorkerEventStop);
  149. furi_thread_join(context->thread);
  150. furi_thread_free(context->thread);
  151. free(context);
  152. }
  153. void totp_generate_code_worker_notify(
  154. TotpGenerateCodeWorkerContext* context,
  155. TotpGenerateCodeWorkerEvent event) {
  156. furi_check(context != NULL);
  157. furi_thread_flags_set(furi_thread_get_id(context->thread), event);
  158. }
  159. void totp_generate_code_worker_set_code_generated_handler(
  160. TotpGenerateCodeWorkerContext* context,
  161. TOTP_NEW_CODE_GENERATED_HANDLER on_new_code_generated_handler,
  162. void* on_new_code_generated_handler_context) {
  163. furi_check(context != NULL);
  164. context->on_new_code_generated_handler = on_new_code_generated_handler;
  165. context->on_new_code_generated_handler_context = on_new_code_generated_handler_context;
  166. }
  167. void totp_generate_code_worker_set_lifetime_changed_handler(
  168. TotpGenerateCodeWorkerContext* context,
  169. TOTP_CODE_LIFETIME_CHANGED_HANDLER on_code_lifetime_changed_handler,
  170. void* on_code_lifetime_changed_handler_context) {
  171. furi_check(context != NULL);
  172. context->on_code_lifetime_changed_handler = on_code_lifetime_changed_handler;
  173. context->on_code_lifetime_changed_handler_context = on_code_lifetime_changed_handler_context;
  174. }