token_info.h 8.3 KB

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