token_info.h 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  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 User-friendly token name
  155. */
  156. FuriString* name;
  157. /**
  158. * @brief Hashing algorithm
  159. */
  160. TokenHashAlgo algo;
  161. /**
  162. * @brief Desired TOTP token length
  163. */
  164. TokenDigitsCount digits;
  165. /**
  166. * @brief Desired TOTP token duration in seconds
  167. */
  168. TokenDuration duration;
  169. /**
  170. * @brief Token input automation features
  171. */
  172. TokenAutomationFeature automation_features;
  173. /**
  174. * @brief Token type
  175. */
  176. TokenType type;
  177. /**
  178. * @brief HOTP counter
  179. */
  180. uint64_t counter;
  181. } TokenInfo;
  182. /**
  183. * @brief Allocates a new instance of \c TokenInfo
  184. * @return
  185. */
  186. TokenInfo* token_info_alloc();
  187. /**
  188. * @brief Disposes all the resources allocated by the given \c TokenInfo instance
  189. * @param token_info instance to be disposed
  190. */
  191. void token_info_free(TokenInfo* token_info);
  192. /**
  193. * @brief Encrypts & sets plain token secret to the given instance of \c TokenInfo
  194. * @param token_info instance where secret should be updated
  195. * @param plain_token_secret plain token secret
  196. * @param token_secret_length plain token secret length
  197. * @param plain_token_secret_encoding plain token secret encoding
  198. * @param crypto_settings crypto settings
  199. * @return \c true if token successfully set; \c false otherwise
  200. */
  201. bool token_info_set_secret(
  202. TokenInfo* token_info,
  203. const char* plain_token_secret,
  204. size_t token_secret_length,
  205. PlainTokenSecretEncoding plain_token_secret_encoding,
  206. const CryptoSettings* crypto_settings);
  207. /**
  208. * @brief Sets token digits count from \c uint8_t value
  209. * @param token_info instance whichs token digits count length should be updated
  210. * @param digits desired token digits count length
  211. * @return \c true if token digits count length has been updated; \c false otherwise
  212. */
  213. bool token_info_set_digits_from_int(TokenInfo* token_info, uint8_t digits);
  214. /**
  215. * @brief Sets token duration from \c uint8_t value
  216. * @param token_info instance whichs token digits count length should be updated
  217. * @param duration desired token duration in seconds
  218. * @return \c true if token duration has been updated; \c false otherwise
  219. */
  220. bool token_info_set_duration_from_int(TokenInfo* token_info, uint8_t duration);
  221. /**
  222. * @brief Sets token hashing algorithm from \c str value
  223. * @param token_info instance whichs token hashing algorithm should be updated
  224. * @param str desired token algorithm
  225. * @return \c true if token hashing algorithm has been updated; \c false otherwise
  226. */
  227. bool token_info_set_algo_from_str(TokenInfo* token_info, const FuriString* str);
  228. /**
  229. * @brief Sets token hashing algorithm from \c algo_code code
  230. * @param token_info instance whichs token hashing algorithm should be updated
  231. * @param algo_code desired token algorithm code
  232. * @return \c true if token hashing algorithm has been updated; \c false otherwise
  233. */
  234. bool token_info_set_algo_from_int(TokenInfo* token_info, uint8_t algo_code);
  235. /**
  236. * @brief Gets token hashing algorithm name as C-string
  237. * @param token_info instance which token hashing algorithm name should be returned
  238. * @return token hashing algorithm name as C-string
  239. */
  240. const char* token_info_get_algo_as_cstr(const TokenInfo* token_info);
  241. /**
  242. * @brief Sets token automation feature from \c str value
  243. * @param token_info instance whichs token automation feature should be updated
  244. * @param str desired token automation feature
  245. * @return \c true if token automation feature has been set; \c false otherwise
  246. */
  247. bool token_info_set_automation_feature_from_str(TokenInfo* token_info, const FuriString* str);
  248. /**
  249. * @brief Sets token type from \c str value
  250. * @param token_info instance whichs token type should be updated
  251. * @param str desired token type
  252. * @return \c true if token type has been set; \c false otherwise
  253. */
  254. bool token_info_set_token_type_from_str(TokenInfo* token_info, const FuriString* str);
  255. /**
  256. * @brief Gets token type as C-string
  257. * @param token_info instance which token type should be returned
  258. * @return token type as C-string
  259. */
  260. const char* token_info_get_type_as_cstr(const TokenInfo* token_info);
  261. /**
  262. * @brief Sets token counter from \c str value
  263. * @param token_info instance whichs token counter should be updated
  264. * @param str desired token counter
  265. * @return \c true if token counter has been set; \c false otherwise
  266. */
  267. bool token_info_set_token_counter_from_str(TokenInfo* token_info, const FuriString* str);
  268. /**
  269. * @brief Clones \c TokenInfo instance
  270. * @param src instance to clone
  271. * @return cloned instance
  272. */
  273. TokenInfo* token_info_clone(const TokenInfo* src);
  274. /**
  275. * @brief Sets default values to all the properties of \c token_info
  276. * @param token_info instance to set defaults to
  277. */
  278. void token_info_set_defaults(TokenInfo* token_info);
  279. #ifdef __cplusplus
  280. }
  281. #endif