token_info.h 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. #pragma once
  2. #include <inttypes.h>
  3. #include <stdbool.h>
  4. #include <furi/core/string.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. #define TOTP_TOKEN_DIGITS_MAX_COUNT (8)
  18. typedef uint8_t TokenHashAlgo;
  19. typedef uint8_t TokenDigitsCount;
  20. typedef uint8_t TokenAutomationFeature;
  21. typedef uint8_t PlainTokenSecretEncoding;
  22. /**
  23. * @brief Hashing algorithm to be used to generate token
  24. */
  25. enum TokenHashAlgos {
  26. /**
  27. * @brief SHA1 hashing algorithm
  28. */
  29. SHA1 = 0,
  30. /**
  31. * @brief SHA256 hashing algorithm
  32. */
  33. SHA256 = 1,
  34. /**
  35. * @brief SHA512 hashing algorithm
  36. */
  37. SHA512 = 2,
  38. /**
  39. * @brief Algorithm used by Steam (Valve)
  40. */
  41. STEAM = 3
  42. };
  43. /**
  44. * @brief Token digits count to be generated.
  45. */
  46. enum TokenDigitsCounts {
  47. /**
  48. * @brief 5 digits
  49. */
  50. TotpFiveDigitsCount = 5,
  51. /**
  52. * @brief 6 digits
  53. */
  54. TotpSixDigitsCount = 6,
  55. /**
  56. * @brief 8 digits
  57. */
  58. TotpEightDigitsCount = 8
  59. };
  60. /**
  61. * @brief Token automation features.
  62. */
  63. enum TokenAutomationFeatures {
  64. /**
  65. * @brief No features enabled
  66. */
  67. TokenAutomationFeatureNone = 0b000,
  68. /**
  69. * @brief Press "Enter" key at the end as a part of token input automation
  70. */
  71. TokenAutomationFeatureEnterAtTheEnd = 0b001,
  72. /**
  73. * @brief Press "Tab" key at the end as a part of token input automation
  74. */
  75. TokenAutomationFeatureTabAtTheEnd = 0b010,
  76. /**
  77. * @brief Press keys slower and wait longer between keystrokes
  78. */
  79. TokenAutomationFeatureTypeSlower = 0b100
  80. };
  81. /**
  82. * @brief Plain token secret encodings.
  83. */
  84. enum PlainTokenSecretEncodings {
  85. /**
  86. * @brief Base32 encoding
  87. */
  88. PlainTokenSecretEncodingBase32 = 0,
  89. /**
  90. * @brief Base64 encoding
  91. */
  92. PlainTokenSecretEncodingBase64 = 1
  93. };
  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. FuriString* 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 plain_token_secret plain token secret
  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 hashing 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 Sets token hashing algorithm from \c algo_code code
  175. * @param token_info instance whichs token hashing algorithm should be updated
  176. * @param algo_code desired token algorithm code
  177. * @return \c true if token hashing algorithm has been updated; \c false otherwise
  178. */
  179. bool token_info_set_algo_from_int(TokenInfo* token_info, uint8_t algo_code);
  180. /**
  181. * @brief Gets token hahsing algorithm name as C-string
  182. * @param token_info instance which token hahsing algorithm name should be returned
  183. * @return token hashing algorithm name as C-string
  184. */
  185. char* token_info_get_algo_as_cstr(const TokenInfo* token_info);
  186. /**
  187. * @brief Sets token automation feature from \c str value
  188. * @param token_info instance whichs token automation feature should be updated
  189. * @param str desired token automation feature
  190. * @return \c true if token automation feature has been set; \c false otherwise
  191. */
  192. bool token_info_set_automation_feature_from_str(TokenInfo* token_info, const FuriString* str);
  193. /**
  194. * @brief Clones \c TokenInfo instance
  195. * @param src instance to clone
  196. * @return cloned instance
  197. */
  198. TokenInfo* token_info_clone(const TokenInfo* src);
  199. /**
  200. * @brief Sets default values to all the properties of \c token_info
  201. * @param token_info instance to set defaults to
  202. */
  203. void token_info_set_defaults(TokenInfo* token_info);