token_info.h 5.6 KB

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