token_info.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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. /**
  81. * @brief Plain token secret encodings.
  82. */
  83. enum PlainTokenSecretEncodings {
  84. /**
  85. * @brief Base32 encoding
  86. */
  87. PLAIN_TOKEN_ENCODING_BASE32 = 0,
  88. /**
  89. * @brief Base64 encoding
  90. */
  91. PLAIN_TOKEN_ENCODING_BASE64 = 1
  92. };
  93. #define TOTP_TOKEN_DIGITS_MAX_COUNT (8)
  94. /**
  95. * @brief TOTP token information
  96. */
  97. typedef struct {
  98. /**
  99. * @brief Encrypted token secret
  100. */
  101. uint8_t* token;
  102. /**
  103. * @brief Encrypted token secret length
  104. */
  105. size_t token_length;
  106. /**
  107. * @brief User-friendly token name
  108. */
  109. char* name;
  110. /**
  111. * @brief Hashing algorithm
  112. */
  113. TokenHashAlgo algo;
  114. /**
  115. * @brief Desired TOTP token length
  116. */
  117. TokenDigitsCount digits;
  118. /**
  119. * @brief Desired TOTP token duration in seconds
  120. */
  121. uint8_t duration;
  122. /**
  123. * @brief Token input automation features
  124. */
  125. TokenAutomationFeature automation_features;
  126. } TokenInfo;
  127. /**
  128. * @brief Allocates a new instance of \c TokenInfo
  129. * @return
  130. */
  131. TokenInfo* token_info_alloc();
  132. /**
  133. * @brief Disposes all the resources allocated by the given \c TokenInfo instance
  134. * @param token_info instance to be disposed
  135. */
  136. void token_info_free(TokenInfo* token_info);
  137. /**
  138. * @brief Encrypts & sets plain token secret to the given instance of \c TokenInfo
  139. * @param token_info instance where secret should be updated
  140. * @param base32_token_secret plain token secret in Base32 format
  141. * @param token_secret_length plain token secret length
  142. * @param plain_token_secret_encoding plain token secret encoding
  143. * @param iv initialization vecor (IV) to be used for encryption
  144. * @return \c true if token successfully set; \c false otherwise
  145. */
  146. bool token_info_set_secret(
  147. TokenInfo* token_info,
  148. const char* plain_token_secret,
  149. size_t token_secret_length,
  150. PlainTokenSecretEncoding plain_token_secret_encoding,
  151. const uint8_t* iv);
  152. /**
  153. * @brief Sets token digits count from \c uint8_t value
  154. * @param token_info instance whichs token digits count length should be updated
  155. * @param digits desired token digits count length
  156. * @return \c true if token digits count length has been updated; \c false otherwise
  157. */
  158. bool token_info_set_digits_from_int(TokenInfo* token_info, uint8_t digits);
  159. /**
  160. * @brief Sets token duration from \c uint8_t value
  161. * @param token_info instance whichs token digits count length should be updated
  162. * @param duration desired token duration in seconds
  163. * @return \c true if token duration has been updated; \c false otherwise
  164. */
  165. bool token_info_set_duration_from_int(TokenInfo* token_info, uint8_t duration);
  166. /**
  167. * @brief Sets token hashing algorithm from \c str value
  168. * @param token_info instance whichs token hashing algorithm should be updated
  169. * @param str desired token algorithm
  170. * @return \c true if token hahsing algorithm has been updated; \c false otherwise
  171. */
  172. bool token_info_set_algo_from_str(TokenInfo* token_info, const FuriString* str);
  173. /**
  174. * @brief Gets token hahsing algorithm name as C-string
  175. * @param token_info instance which token hahsing algorithm name should be returned
  176. * @return token hashing algorithm name as C-string
  177. */
  178. char* token_info_get_algo_as_cstr(const TokenInfo* token_info);
  179. /**
  180. * @brief Sets token automation feature from \c str value
  181. * @param token_info instance whichs token automation feature should be updated
  182. * @param str desired token automation feature
  183. * @return \c true if token automation feature has been set; \c false otherwise
  184. */
  185. bool token_info_set_automation_feature_from_str(TokenInfo* token_info, const FuriString* str);
  186. /**
  187. * @brief Clones \c TokenInfo instance
  188. * @param src instance to clone
  189. * @return cloned instance
  190. */
  191. TokenInfo* token_info_clone(const TokenInfo* src);