token_info.h 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. #pragma once
  2. #include <inttypes.h>
  3. #include <stdbool.h>
  4. #include <furi/core/string.h>
  5. #define TOKEN_HASH_ALGO_SHA1_NAME "sha1"
  6. #define TOKEN_HASH_ALGO_STEAM_NAME "steam"
  7. #define TOKEN_HASH_ALGO_SHA256_NAME "sha256"
  8. #define TOKEN_HASH_ALGO_SHA512_NAME "sha512"
  9. #define PLAIN_TOKEN_ENCODING_BASE32_NAME "base32"
  10. #define PLAIN_TOKEN_ENCODING_BASE64_NAME "base64"
  11. #define TOKEN_AUTOMATION_FEATURE_NONE_NAME "none"
  12. #define TOKEN_AUTOMATION_FEATURE_ENTER_AT_THE_END_NAME "enter"
  13. #define TOKEN_AUTOMATION_FEATURE_TAB_AT_THE_END_NAME "tab"
  14. #define TOKEN_AUTOMATION_FEATURE_TYPE_SLOWER_NAME "slower"
  15. typedef uint8_t TokenHashAlgo;
  16. typedef uint8_t TokenDigitsCount;
  17. typedef uint8_t TokenDuration;
  18. typedef uint8_t TokenAutomationFeature;
  19. typedef uint8_t PlainTokenSecretEncoding;
  20. /**
  21. * @brief Hashing algorithm to be used to generate token
  22. */
  23. enum TokenHashAlgos {
  24. /**
  25. * @brief SHA1 hashing algorithm
  26. */
  27. TokenHashAlgoSha1 = 0,
  28. /**
  29. * @brief SHA256 hashing algorithm
  30. */
  31. TokenHashAlgoSha256 = 1,
  32. /**
  33. * @brief SHA512 hashing algorithm
  34. */
  35. TokenHashAlgoSha512 = 2,
  36. /**
  37. * @brief Algorithm used by Steam (Valve)
  38. */
  39. TokenHashAlgoSteam = 3,
  40. /**
  41. * @brief Default token hashing algorithm
  42. */
  43. TokenHashAlgoDefault = TokenHashAlgoSha1
  44. };
  45. /**
  46. * @brief Token digits count to be generated.
  47. */
  48. enum TokenDigitsCounts {
  49. /**
  50. * @brief 5 digits
  51. */
  52. TokenDigitsCountFive = 5,
  53. /**
  54. * @brief 6 digits
  55. */
  56. TokenDigitsCountSix = 6,
  57. /**
  58. * @brief 8 digits
  59. */
  60. TokenDigitsCountEight = 8,
  61. /**
  62. * @brief Default digits count
  63. */
  64. TokenDigitsCountDefault = TokenDigitsCountSix,
  65. /**
  66. * @brief Maximum digits count
  67. */
  68. TokenDigitsCountMax = TokenDigitsCountEight
  69. };
  70. /**
  71. * @brief Token durations
  72. */
  73. enum TokenDurations {
  74. /**
  75. * @brief Default token duration
  76. */
  77. TokenDurationDefault = 30,
  78. /**
  79. * @brief Minimum token duration
  80. */
  81. TokenDurationMin = 15,
  82. /**
  83. * @brief Maximum token duration
  84. */
  85. TokenDurationMax = UINT8_MAX
  86. };
  87. /**
  88. * @brief Token automation features.
  89. */
  90. enum TokenAutomationFeatures {
  91. /**
  92. * @brief No features enabled
  93. */
  94. TokenAutomationFeatureNone = 0b000,
  95. /**
  96. * @brief Press "Enter" key at the end as a part of token input automation
  97. */
  98. TokenAutomationFeatureEnterAtTheEnd = 0b001,
  99. /**
  100. * @brief Press "Tab" key at the end as a part of token input automation
  101. */
  102. TokenAutomationFeatureTabAtTheEnd = 0b010,
  103. /**
  104. * @brief Press keys slower and wait longer between keystrokes
  105. */
  106. TokenAutomationFeatureTypeSlower = 0b100
  107. };
  108. /**
  109. * @brief Plain token secret encodings.
  110. */
  111. enum PlainTokenSecretEncodings {
  112. /**
  113. * @brief Base32 encoding
  114. */
  115. PlainTokenSecretEncodingBase32 = 0,
  116. /**
  117. * @brief Base64 encoding
  118. */
  119. PlainTokenSecretEncodingBase64 = 1
  120. };
  121. /**
  122. * @brief TOTP token information
  123. */
  124. typedef struct {
  125. /**
  126. * @brief Encrypted token secret
  127. */
  128. uint8_t* token;
  129. /**
  130. * @brief Encrypted token secret length
  131. */
  132. size_t token_length;
  133. /**
  134. * @brief User-friendly token name
  135. */
  136. FuriString* name;
  137. /**
  138. * @brief Hashing algorithm
  139. */
  140. TokenHashAlgo algo;
  141. /**
  142. * @brief Desired TOTP token length
  143. */
  144. TokenDigitsCount digits;
  145. /**
  146. * @brief Desired TOTP token duration in seconds
  147. */
  148. TokenDuration duration;
  149. /**
  150. * @brief Token input automation features
  151. */
  152. TokenAutomationFeature automation_features;
  153. } TokenInfo;
  154. /**
  155. * @brief Allocates a new instance of \c TokenInfo
  156. * @return
  157. */
  158. TokenInfo* token_info_alloc();
  159. /**
  160. * @brief Disposes all the resources allocated by the given \c TokenInfo instance
  161. * @param token_info instance to be disposed
  162. */
  163. void token_info_free(TokenInfo* token_info);
  164. /**
  165. * @brief Encrypts & sets plain token secret to the given instance of \c TokenInfo
  166. * @param token_info instance where secret should be updated
  167. * @param plain_token_secret plain token secret
  168. * @param token_secret_length plain token secret length
  169. * @param plain_token_secret_encoding plain token secret encoding
  170. * @param iv initialization vecor (IV) to be used for encryption
  171. * @param crypto_version crypto algorithm version to be used
  172. * @param crypto_key_slot crypto key slot to be used
  173. * @return \c true if token successfully set; \c false otherwise
  174. */
  175. bool token_info_set_secret(
  176. TokenInfo* token_info,
  177. const char* plain_token_secret,
  178. size_t token_secret_length,
  179. PlainTokenSecretEncoding plain_token_secret_encoding,
  180. const uint8_t* iv,
  181. uint8_t crypto_version,
  182. uint8_t crypto_key_slot);
  183. /**
  184. * @brief Sets token digits count from \c uint8_t value
  185. * @param token_info instance whichs token digits count length should be updated
  186. * @param digits desired token digits count length
  187. * @return \c true if token digits count length has been updated; \c false otherwise
  188. */
  189. bool token_info_set_digits_from_int(TokenInfo* token_info, uint8_t digits);
  190. /**
  191. * @brief Sets token duration from \c uint8_t value
  192. * @param token_info instance whichs token digits count length should be updated
  193. * @param duration desired token duration in seconds
  194. * @return \c true if token duration has been updated; \c false otherwise
  195. */
  196. bool token_info_set_duration_from_int(TokenInfo* token_info, uint8_t duration);
  197. /**
  198. * @brief Sets token hashing algorithm from \c str value
  199. * @param token_info instance whichs token hashing algorithm should be updated
  200. * @param str desired token algorithm
  201. * @return \c true if token hashing algorithm has been updated; \c false otherwise
  202. */
  203. bool token_info_set_algo_from_str(TokenInfo* token_info, const FuriString* str);
  204. /**
  205. * @brief Sets token hashing algorithm from \c algo_code code
  206. * @param token_info instance whichs token hashing algorithm should be updated
  207. * @param algo_code desired token algorithm code
  208. * @return \c true if token hashing algorithm has been updated; \c false otherwise
  209. */
  210. bool token_info_set_algo_from_int(TokenInfo* token_info, uint8_t algo_code);
  211. /**
  212. * @brief Gets token hahsing algorithm name as C-string
  213. * @param token_info instance which token hahsing algorithm name should be returned
  214. * @return token hashing algorithm name as C-string
  215. */
  216. const char* token_info_get_algo_as_cstr(const TokenInfo* token_info);
  217. /**
  218. * @brief Sets token automation feature from \c str value
  219. * @param token_info instance whichs token automation feature should be updated
  220. * @param str desired token automation feature
  221. * @return \c true if token automation feature has been set; \c false otherwise
  222. */
  223. bool token_info_set_automation_feature_from_str(TokenInfo* token_info, const FuriString* str);
  224. /**
  225. * @brief Clones \c TokenInfo instance
  226. * @param src instance to clone
  227. * @return cloned instance
  228. */
  229. TokenInfo* token_info_clone(const TokenInfo* src);
  230. /**
  231. * @brief Sets default values to all the properties of \c token_info
  232. * @param token_info instance to set defaults to
  233. */
  234. void token_info_set_defaults(TokenInfo* token_info);