generate_totp_code.c 6.9 KB

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