token_info.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. #pragma once
  2. #include <inttypes.h>
  3. #include <stdbool.h>
  4. #include <furi/furi.h>
  5. #define TOTP_TOKEN_DURATION_DEFAULT 30
  6. #define TOTP_TOKEN_ALGO_SHA1_NAME "sha1"
  7. #define TOTP_TOKEN_ALGO_SHA256_NAME "sha256"
  8. #define TOTP_TOKEN_ALGO_SHA512_NAME "sha512"
  9. #define TOTP_TOKEN_MAX_LENGTH 255
  10. #define TOTP_TOKEN_AUTOMATION_FEATURE_NONE_NAME "none"
  11. #define TOTP_TOKEN_AUTOMATION_FEATURE_ENTER_AT_THE_END_NAME "enter"
  12. #define TOTP_TOKEN_AUTOMATION_FEATURE_TAB_AT_THE_END_NAME "tab"
  13. #define TOTP_TOKEN_AUTOMATION_FEATURE_TYPE_SLOWER_NAME "slower"
  14. typedef uint8_t TokenHashAlgo;
  15. typedef uint8_t TokenDigitsCount;
  16. typedef uint8_t TokenAutomationFeature;
  17. /**
  18. * @brief Hashing algorithm to be used to generate token
  19. */
  20. enum TokenHashAlgos {
  21. /**
  22. * @brief SHA1 hashing algorithm
  23. */
  24. SHA1,
  25. /**
  26. * @brief SHA256 hashing algorithm
  27. */
  28. SHA256,
  29. /**
  30. * @brief SHA512 hashing algorithm
  31. */
  32. SHA512
  33. };
  34. /**
  35. * @brief Token digits count to be generated.
  36. */
  37. enum TokenDigitsCounts {
  38. /**
  39. * @brief 6 digits
  40. */
  41. TOTP_6_DIGITS = 6,
  42. /**
  43. * @brief 8 digits
  44. */
  45. TOTP_8_DIGITS = 8
  46. };
  47. /**
  48. * @brief Token automation features.
  49. */
  50. enum TokenAutomationFeatures {
  51. /**
  52. * @brief No features enabled
  53. */
  54. TOKEN_AUTOMATION_FEATURE_NONE = 0b000,
  55. /**
  56. * @brief Press "Enter" key at the end as a part of token input automation
  57. */
  58. TOKEN_AUTOMATION_FEATURE_ENTER_AT_THE_END = 0b001,
  59. /**
  60. * @brief Press "Tab" key at the end as a part of token input automation
  61. */
  62. TOKEN_AUTOMATION_FEATURE_TAB_AT_THE_END = 0b010,
  63. /**
  64. * @brief Press keys slower and wait longer between keystrokes
  65. */
  66. TOKEN_AUTOMATION_FEATURE_TYPE_SLOWER = 0b100
  67. };
  68. #define TOTP_TOKEN_DIGITS_MAX_COUNT 8
  69. /**
  70. * @brief TOTP token information
  71. */
  72. typedef struct {
  73. /**
  74. * @brief Encrypted token secret
  75. */
  76. uint8_t* token;
  77. /**
  78. * @brief Encrypted token secret length
  79. */
  80. size_t token_length;
  81. /**
  82. * @brief User-friendly token name
  83. */
  84. char* name;
  85. /**
  86. * @brief Hashing algorithm
  87. */
  88. TokenHashAlgo algo;
  89. /**
  90. * @brief Desired TOTP token length
  91. */
  92. TokenDigitsCount digits;
  93. /**
  94. * @brief Desired TOTP token duration in seconds
  95. */
  96. uint8_t duration;
  97. /**
  98. * @brief Token input automation features
  99. */
  100. TokenAutomationFeature automation_features;
  101. } TokenInfo;
  102. /**
  103. * @brief Allocates a new instance of \c TokenInfo
  104. * @return
  105. */
  106. TokenInfo* token_info_alloc();
  107. /**
  108. * @brief Disposes all the resources allocated by the given \c TokenInfo instance
  109. * @param token_info instance to be disposed
  110. */
  111. void token_info_free(TokenInfo* token_info);
  112. /**
  113. * @brief Encrypts & sets plain token secret to the given instance of \c TokenInfo
  114. * @param token_info instance where secret should be updated
  115. * @param base32_token_secret plain token secret in Base32 format
  116. * @param token_secret_length plain token secret length
  117. * @param iv initialization vecor (IV) to be used for encryption
  118. * @return \c true if token successfully set; \c false otherwise
  119. */
  120. bool token_info_set_secret(
  121. TokenInfo* token_info,
  122. const char* base32_token_secret,
  123. size_t token_secret_length,
  124. const uint8_t* iv);
  125. /**
  126. * @brief Sets token digits count from \c uint8_t value
  127. * @param token_info instance whichs token digits count length should be updated
  128. * @param digits desired token digits count length
  129. * @return \c true if token digits count length has been updated; \c false otherwise
  130. */
  131. bool token_info_set_digits_from_int(TokenInfo* token_info, uint8_t digits);
  132. /**
  133. * @brief Sets token duration from \c uint8_t value
  134. * @param token_info instance whichs token digits count length should be updated
  135. * @param duration desired token duration in seconds
  136. * @return \c true if token duration has been updated; \c false otherwise
  137. */
  138. bool token_info_set_duration_from_int(TokenInfo* token_info, uint8_t duration);
  139. /**
  140. * @brief Sets token hashing algorithm from \c str value
  141. * @param token_info instance whichs token hashing algorithm should be updated
  142. * @param str desired token algorithm
  143. * @return \c true if token hahsing algorithm has been updated; \c false otherwise
  144. */
  145. bool token_info_set_algo_from_str(TokenInfo* token_info, const FuriString* str);
  146. /**
  147. * @brief Gets token hahsing algorithm name as C-string
  148. * @param token_info instance which token hahsing algorithm name should be returned
  149. * @return token hashing algorithm name as C-string
  150. */
  151. char* token_info_get_algo_as_cstr(const TokenInfo* token_info);
  152. /**
  153. * @brief Sets token automation feature from \c str value
  154. * @param token_info instance whichs token automation feature should be updated
  155. * @param str desired token automation feature
  156. * @return \c true if token automation feature has been set; \c false otherwise
  157. */
  158. bool token_info_set_automation_feature_from_str(TokenInfo* token_info, const FuriString* str);
  159. /**
  160. * @brief Clones \c TokenInfo instance
  161. * @param src instance to clone
  162. * @return cloned instance
  163. */
  164. TokenInfo* token_info_clone(const TokenInfo* src);