generate_totp_code.c 7.1 KB

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