token_info.h 6.8 KB

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