generate_totp_code.c 6.7 KB

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